java thread类_java多线程:Thread类介绍,实现方式,示例

java是一个多线程的编程语言,就是说在java的应用中可以并行的执行多个线程,每个线程可以执行不同的操作。在单cpu的机器上多线程会分享cpu时间,而在多线程的机器上不同线程可以使用不同的CPU。

java线程的生命周期

java线程在他的生命周期内有几种不同的状态:线程初始化,启动,运行和死亡。

AAffA0nNPuCLAAAAAElFTkSuQmCC

上图所示的状态解释如下:new 是指线程被初始化,但是还没有调用其start方法,还没有开始执行

runnable 调用线程的start方法之后,线程开始执行其任务,这时候线程是运行状态

waiting 有时候线程需要等待另外一个线程执行完毕之后再执行,这时候线程处于等待状态,处于等待状态的线程需要其他线程notify之后才能恢复到运行状态

timed waiting 运行中的线程可以进入到定时等待的状态,这时候线程间隔指定的时间间隔之后就会恢复到运行状态

terminated 当线程任务执行完毕或者被abort的时候线程处于终止状态

java线程的优先级

每一个java线程都有一个优先级,操作系统可以通过线程的优先级决定决定将cpu分配给哪个线程。优先级越高的线程越可能得到cpu资源。

java线程优先级的值在1-10之间,1是常量MIN_PRIORITY,10是常量MAX_PRIORITY 。默认情况下java的线程的优先级是NORM_PRIORITY 即5.

高优先级的线程通常更重要,更有可能获得cpu时间资源,但是并不能保证绝对可以获得cpu。

java多线程实现的两种方式

1. 使用Runnable接口实现多线程

使用Runnable接口实现多线程需要两个步骤,首先实现Runnable接口类,然后声明Thread实例,调用thread实例的start方法,开始执行。

如下代码示例:package cn.outofmemory.java.example; public class RunTask implements Runnable { @Override public void run() { for (int i=0;i<10;i++) { System.out.println("hello counter " + i); try { Thread.sleep(20); } catch (InterruptedException e) { ; } } } }

package cn.outofmemory.java.example; public class App { public static void main(String[] args) { for (int i = 0; i 

上面的RunTask类实现了Runnable的接口,App类的main方法for循环生成Runnable示例,并将其传递给Thread示例的构造函数中,然后调用start方法启动线程。

上面程序将输出:hello counter 0 hello counter 0 hello counter 1 hello counter 1 hello counter 2 hello counter 2 hello counter 3 hello counter 3 hello counter 4 hello counter 4 hello counter 5 hello counter 5 hello counter 6 hello counter 6 hello counter 7 hello counter 7 hello counter 8 hello counter 8 hello counter 9 hello counter 9

可以看到两个线程在并行的执行。

2. 从Thread类继承实现java的多线程

从java的Thread类继承实现多线程,也是实现其run方法,然后声明实例,并调用实例的start方法启动线程。

代码示例如下:package cn.outofmemory.java.example; public class SimpleThread extends Thread { public void run() { for (int i=0;i<10;i++) { System.out.println("thread " + this.getId() + " print out " + i); try { Thread.sleep(0); } catch (InterruptedException e) { ; } } } }

package cn.outofmemory.java.example; public class App { public static void main(String[] args) { for (int i = 0; i 

SimpleThead类从Thread类继承,并重写了run方法。 在App类的main方法中for循环new了两个SimpleThread的实例,并调用其start方法。

程序输出类似于:thread 12 print out 0 thread 11 print out 0 thread 12 print out 1 thread 12 print out 2 thread 12 print out 3 thread 12 print out 4 thread 12 print out 5 thread 12 print out 6 thread 11 print out 1 thread 11 print out 2 thread 12 print out 7 thread 11 print out 3 thread 12 print out 8 thread 12 print out 9 thread 11 print out 4 thread 11 print out 5 thread 11 print out 6 thread 11 print out 7 thread 11 print out 8 thread 11 print out 9

java Thread类的主要方法介绍

Thread的实例方法:方法定义方法说明

public void start()最常用的方法,顾名思义启动线程,即开始执行线程的run方法

public void run()如果线程重写了run方法,那么执行重写的方法,否则执行线程的Runnable接口中定义的run方法

public final void setName(String)设置线程的名称

public final void setPriority(int)设置线程的优先级(范围在1-10包含1,10)

public final void setDeamon(boolean)设置线程是否是后台线程

public final void join(long)在另外一个线程中调用当前线程的join方法,会导致当前线程阻塞,直到另一线程执行完毕,或者超过参数指定毫秒数

public void interrupt()中断线程

public final boolean isAlive()线程是否处于存活状态,线程在启动和结束之前都处于存活状态

Thread类的常用静态方法:方法定义方法说明public static void yield()使当前运行线程相同优先级的线程获得执行机会,类似sleep,但是只会将cpu让给相同优先级的线程

public static void sleep(long)使当前线程休眠指定毫秒的时间

public static boolean holdsLock(Object x)判断当前线程是否拥有对象的锁

public static Thread currentThread()获得当前线程实例

public static void dumpStack()打印当前线程的执行堆栈,这对多线程程序的调试很有帮助

java线程示例1: 如何使用interupt方法中断线程public class GeneralInterrupt extends Object  implements Runnable {    public void run() {       try {          System.out.println("in run() - about to work2()");          work2();          System.out.println("in run() - back from  work2()");       }       catch (InterruptedException x) {          System.out.println("in run() -  interrupted in work2()");          return;       }       System.out.println("in run() - doing stuff after nap");       System.out.println("in run() - leaving normally");    }    public void work2() throws InterruptedException {       while (true) {          if (Thread.currentThread().isInterrupted()) {             System.out.println("C isInterrupted()="             + Thread.currentThread().isInterrupted());             Thread.sleep(2000);             System.out.println("D isInterrupted()="             + Thread.currentThread().isInterrupted());          }       }    }    public void work() throws InterruptedException {       while (true) {          for (int i = 0; i 

以上程序输出:in run() - about to work2() in main() - interrupting other thread in main() - leaving C isInterrupted()=true in run() - interrupted in work2()

java线程示例2:后台线程示例package cn.outofmemory.java.example; public class DaemonThread implements Runnable { public void run() { System.out.println("entering run()"); try { System.out.println("in run(): currentThread() is" + Thread.currentThread()); while (true) { try { Thread.sleep(500); } catch (InterruptedException x) { } System.out.println("in run(): woke up again"); } } finally { System.out.println("leaving run()"); } } public static void main(String[] args) { System.out.println("entering main()"); Thread t = new Thread(new DaemonThread()); t.setDaemon(true); t.start(); try { Thread.sleep(3000); } catch (InterruptedException x) { } System.out.println("leaving main()"); } }

程序运行将输出如下内容:entering main() entering run() in run(): currentThread() isThread[Thread-0,5,main] in run(): woke up again in run(): woke up again in run(): woke up again in run(): woke up again in run(): woke up again leaving main() in run(): woke up again

java多线程还有很多高级的内容,本文只是阐述了Thread类的基础用法。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值