线程状态概述
线程由生到死的完整过程:
当线程被创建并启动以后,它既不是一启动就进入了执行状态,也不是一直处于执行状态。在线程的生命周期中,有几种状态呢?在API中 java.lang.Thread.State 这个枚举中给出了六种线程状态:
NEW(新建) | 线程刚被创建,但是并未启动。还没调用start方法。MyThread t = new MyThread只有线程对象,没有线程特征。 |
Runnable(可运行) | 线程可以在java虚拟机中运行的状态,可能正在运行自己代码,也可能没有,这取决于操作系统处理器。调用了t.start()方法 :就绪(经典教法) |
Blocked(锁阻塞) | 当一个线程试图获取一个对象锁,而该对象锁被其他的线程持有,则该线程进入Blocked状态;当该线程持有锁时,该线程将变成Runnable状态。 |
Waiting(无限等待) | 一个线程在等待另一个线程执行一个(唤醒)动作时,该线程进入Waiting状态。进入这个状态后是不能自动唤醒的,必须等待另一个线程调用notify或者notifyAll方法才能够唤醒。 |
Timed Waiting(计时等待) | 同waiting状态,有几个方法有超时参数,调用他们将进入Timed Waiting状态。这一状态将一直保持到超时期满或者接收到唤醒通知。带有超时参数的常用方法有Thread.sleep 、Object.wait。 |
Teminated(被终止) | 因为run方法正常退出而死亡,或者因为没有捕获的异常终止了run方法而死亡。 |
睡眠sleep方法
状态中有一个状态叫做计时等待,可以通过Thread类的方法来进行演示。 public static void sleep(long time) 让当前线程进入到睡眠状态,到毫秒后自动醒来继续执行
1 2 3 4 5 6 7 8 9 |
//主线程执行到sleep方法会休眠1秒后再继续执行 public class Test{ public static void main(String[] args){ for ( int i = 1 ;i<= 5 ;i++){ Thread.sleep( 1000 ); System.out.println(i) } } } |
等待和唤醒
public void wait() : 让当前线程进入到等待状态 此方法必须锁对象调用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Demo1_wait { public static void main(String[] args) throws InterruptedException { // 步骤1 : 子线程开启,进入无限等待状态, 没有被唤醒,无法继续运行. new Thread(() -> { try {
System.out.println( "begin wait ...." ); synchronized ( "" ) { "" .wait(); } System.out.println( "over" ); } catch (Exception e) { } }).start(); } |
public void notify() : 唤醒当前锁对象上等待状态的线程 此方法必须锁对象调用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public class Demo2_notify { public static void main(String[] args) throws InterruptedException { // 步骤1 : 子线程开启,进入无限等待状态, 没有被唤醒,无法继续运行. new Thread(() -> { try {
System.out.println( "begin wait ...." ); synchronized ( "" ) { "" .wait(); } System.out.println( "over" ); } catch (Exception e) { } }).start();
//步骤2: 加入如下代码后, 3秒后,会执行notify方法, 唤醒wait中线程. Thread.sleep( 3000 ); new Thread(() -> { try { synchronized ( "" ) { System.out.println( "唤醒" ); "" .notify(); } } catch (Exception e) { } }).start(); } } |
等待唤醒的一个小例子
定义一个集合,包子铺线程完成生产包子,包子添加到集合中;吃货线程完成购买包子,包子从集合中移除。
当包子没有时(包子状态为false),吃货线程等待. 包子铺线程生产包子(即包子状态为true),并通知吃货线程(解除吃货的等待状态)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
public class BaoZiPu extends Thread{ private List<String> list ; public BaoZiPu(String name,ArrayList<String> list){ super (name); this .list = list; } @Override public void run() { int i = 0 ; while ( true ){ //list作为锁对象 synchronized (list){ if (list.size()> 0 ){ //存元素的线程进入到等待状态 try { list.wait(); } catch (InterruptedException e) { e.printStackTrace(); } }
//如果线程没进入到等待状态 说明集合中没有元素 //向集合中添加元素 list.add( "包子" +i++); System.out.println(list); //集合中已经有元素了 唤醒获取元素的线程 list.notify(); } } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
public class ChiHuo extends Thread {
private List<String> list ; public ChiHuo(String name,ArrayList<String> list){ super (name); this .list = list; }
@Override public void run() { //为了能看到效果 写个死循环 while ( true ){ //由于使用的同一个集合 list作为锁对象 synchronized (list){ //如果集合中没有元素 获取元素的线程进入到等待状态 if (list.size()== 0 ){ try { list.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } //如果集合中有元素 则获取元素的线程获取元素(删除) list.remove( 0 ); //打印集合 集合中没有元素了 System.out.println(list); //集合中已经没有元素 则唤醒添加元素的线程 向集合中添加元素 list.notify(); } } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Demo { public static void main(String[] args) { //等待唤醒案例 List<String> list = new ArrayList<>(); // 创建线程对象 BaoZiPu bzp = new BaoZiPu("包子铺",list); ChiHuo ch = new ChiHuo("吃货",list); // 开启线程 bzp.start(); ch.start(); } } |
到此这篇关于Java并发编程之线程状态介绍的文章就介绍到这了,更多相关Java 线程状态内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://juejin.cn/post/7026337429573353486