手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

将线程由冻结状态切换到阻塞状态

时间:2024-11-16 19:50:01

1、interrupt 线程中断技术,实际上就是用强制的方式来改变线程的状态,将处于冻结状态的线程,由冻结状态--->阻塞状态;

2、冻结状态:sleep(time),wait() ,没有执行资格,没有执行权

3、阻塞状态:具备执行资格,正等待CPU执行权

4、sleep(time) 必须待时间到才能醒,如果interrupt,则睡眠状态被清除,立即醒;

5、wait() 由notify()/notifyAll()来唤醒,如果interrupt,则等待状态被清除,不再等待;

6、interrupte蟠校盯昂dException异常;强制将处于冻结状态的线程改变为阻塞状态,使其具备CPU执行资飙空蒈开格;由于这个强制动作破坏了程序预期的执行过程,将抛出interruptedException异常。此时,就需要在catch到interruptedException时,重新对线程的运行环境进行设置;

7、如何停止处于冻结状态的线程---使用中断技术

8、要让线程停止,即让run()执行结束诋危族枧;通常设置标记来控制线程是否继续执行;如果多线程都处于了冻结状态,则无法读取标记进行判断,此时就需要使用中断技术,强鲽判砦吟制解除其冻结状态,回到运行状态去读取新的标记值,达到结束线程的目的!

9、package com.gc.thread;/*** 中断:* 不是将线程停止,而是强制解除睡眠或等待状态的线程,将其状态切换到阻塞状态,具备执行资格* 当被分配到CPU执行权后,将继续执行catch-InterruptedException块中的代码,此处可以做一些控制:* 改变运行环境中的资源,让线程继续执行;* 改变标记,控制线程是否继续执行;* 其它...*/public class InterruptThread implements Runnable{boolean flag = true;public void changeFlag() { flag = false;}public void run() { synchronized(this) { while(flag) { try { System.out.println(Thread.currentThread().getName()+"---waiting"); //线程处于wait状态,释放锁,让其它线程可以进入 wait(); } catch(InterruptedException e) { System.out.println(Thread.currentThread().getName()+"---interrupted"); //线程被中断,回到阻塞状态,当其获得CPU执行权后,在此处改变多线程执行标记,将其停止! changeFlag(); } } System.out.println(Thread.currentThread().getName()+"---"+"over!"); }}public static void main(String[] args) throws InterruptedException { InterruptThread runnable = new InterruptThread(); Thread t1 = new Thread(runnable); Thread t2 = new Thread(runnable); t1.start(); t2.start(); //让t1和t2都处于冻结状态 Thread.sleep(10); t1.interrupt();//解除t1线程的冻结状态,此时t1线程抛出InterruptedException,由catch块捕获,对标记进行改变 t2.interrupt();//解除t2线程的冻结状态,此时t2线程抛出InterruptedException,由catch块捕获,对标记进行改变}}

© 手抄报圈