做网站在手机显示怎么很乱太原建站
做网站在手机显示怎么很乱,太原建站,网站建设规定,成都网站建设询q479185700上快一 中断线程
1.1 中断概念
1.在java中#xff0c;没有提供一种立即停止一条线程。但却给了停止线程的协商机制-中断。
中断是一种协商机制。中断的过程完全需要程序员自己实现。也即#xff0c;如果要中断一个线程#xff0c;你需要手动调用该线程的interrupt()方法…一 中断线程
1.1 中断概念
1.在java中没有提供一种立即停止一条线程。但却给了停止线程的协商机制-中断。
中断是一种协商机制。中断的过程完全需要程序员自己实现。也即如果要中断一个线程你需要手动调用该线程的interrupt()方法该方法也仅仅是将线程对象中的中断标识设置成true接着需要自己在方法中手动判断当前线程的标识位如果为true则中断线程。false标识未中断。
2.一个线程不应该由其他线程中断或者停止而是有该线程自己自行停止自己决定命运。
stop(),suspend,resume方法都已经废弃。
1.2 interrupt和isinterrupted和interrupted的作用
1.public void interrupt(): 实例方法仅仅是设置线程中断状态为true,发起一个协商而不会立刻停止线程。
2.public static boolean interrupted静态方法判断当前线程是否中断并清除当前线程的中断状态。做两件事
a)返回当前线程的中断状态测试当前线程是否已经中断。
b)将当前线程的中断状态清零并重新设置为true清除线程的中断状态。
3.public boolean isinterrupted():实例方法判断当前线程是不是中断检测中断标志位。
1.3 interrupt的使用情况分析
当对一个线程调用interrupt()时
1.如果线程处于正常活动状态那么会将该线程的中断标志设置为true仅此而已。
被设置中断标志的线程将继续正常运行不受影响。
interrupt并不能真正中断线程需要被调用的线程自己进行配合才行。
2.如果线程处于被阻塞状态(sleep,wait,join等状态)在别的线程中调用当前线程对象的interrupt方法那么线程将立即退出被阻塞的状态并抛出一个interruptedException异常。
二 中断线程方式
2.1 方式1 通过volatile变量
package com.ljf.thread.interrupt;import java.util.concurrent.atomic.AtomicBoolean;/*** ClassName: Zhongduan2* Description: TODO* Author: admin* Date: 2024/03/02 17:43:01 * Version: V1.0**/
public class Zhongduan2 {public static volatile boolean flagfalse;static AtomicBoolean atomicBoolean new AtomicBoolean(false);public static void main(String[] args) throws InterruptedException {Thread t1 new Thread(new Runnable() {Overridepublic void run() {while(true){if(flag){// if(atomicBoolean.get()){System.out.println(开始中断Thread.currentThread().getId());break;}else{System.out.println(循环中:Thread.currentThread().getId());}}}});t1.start();//主线程先休眠2秒Thread.sleep(5000);new Thread(new Runnable() {Overridepublic void run() {flagtrue;atomicBoolean.set(true);}}).start();}}2.2 方式2 通过atomicboolean 原子类
public static volatile boolean flagfalse;static AtomicBoolean atomicBoolean new AtomicBoolean(false);public static void main(String[] args) throws InterruptedException {Thread t1 new Thread(new Runnable() {Overridepublic void run() {while(true){// if(flag){if(atomicBoolean.get()){System.out.println(开始中断Thread.currentThread().getId());break;}else{System.out.println(循环中:Thread.currentThread().getId());}}}});t1.start();//主线程先休眠2秒Thread.sleep(5000);new Thread(new Runnable() {Overridepublic void run() {flagtrue;atomicBoolean.set(true);}}).start();}
2.3 方式3 通过interruptisinterrupted组合判断
package com.ljf.thread.interrupt;/*** ClassName: Zhongduan* Description: TODO* Author: admin* Date: 2024/03/02 17:34:59 * Version: V1.0**/
public class Zhongduan {public static volatile boolean flagfalse;public static void main(String[] args) throws InterruptedException {Thread t1 new Thread(new Runnable() {Overridepublic void run() {while(true){if(Thread.currentThread().isInterrupted()){System.out.println(开始中断Thread.currentThread().getId());break;}else{System.out.println(循环中:Thread.currentThread().getId());}}}});t1.start();//主线程先休眠2秒Thread.sleep(5000);new Thread(new Runnable() {Overridepublic void run() {t1.interrupt();}}).start();}
}三 中断线程方式的情况分析
3.1 情况1
如果线程处于被阻塞状态(sleep,wait,join等状态)在别的线程中调用当前线程对象的interrupt方法那么线程将立即退出被阻塞的状态并抛出一个interruptedException异常。 public static void main(String[] args){//实例方法interrupt()仅仅是设置线程的中断状态位设置为true不会停止线程Thread t1 new Thread(() - {for (int i 1; i 300; i){System.out.println(-----: i);}System.out.println(t1线程调用interrupt()后的的中断标识02Thread.currentThread().isInterrupted());}, t1);t1.start();System.out.println(t1线程默认的中断标识t1.isInterrupted());//false//暂停毫秒try { TimeUnit.MILLISECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); }t1.interrupt();//trueSystem.out.println(t1线程调用interrupt()后的的中断标识01t1.isInterrupted());//truetry { TimeUnit.MILLISECONDS.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); }System.out.println(t1线程调用interrupt()后的的中断标识03t1.isInterrupted());//---false中断不活动的线程不会产生任何影响。}
线程t1执行300次后已经不存在2秒后查看t1线程的状态为false。 3.2 情况2
1.代码
public class InterruptDemo3
{public static void main(String[] args){Thread t1 new Thread(() - {while (true){if(Thread.currentThread().isInterrupted()){System.out.println(Thread.currentThread().getName()\t 中断标志位Thread.currentThread().isInterrupted() 程序停止);break;}try {Thread.sleep(200);} catch (InterruptedException e) {Thread.currentThread().interrupt();//为什么要在异常处再调用一次e.printStackTrace();}System.out.println(-----hello InterruptDemo3);}}, t1);t1.start();//暂停几秒钟线程try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }new Thread(() - t1.interrupt(),t2).start();}
}
问题报异常程序没有停止 2.分析原因
* 1 中断标志位默认false
* 2 t2 ---- t1发出了中断协商t2调用t1.interrupt()中断标志位true
* 3 中断标志位true正常情况程序停止^_^
* 4 中断标志位true执行sleep 后发生异常情况InterruptedException将会把中断状态将被清除并且将收到InterruptedException 。中断标志位false
* 导致无限循环
*
* 5 在catch块中需要再次给中断标志位设置为true2次调用停止程序才OK
3.结论 3.3 情况3 关闭线程
https://blog.51cto.com/u_13316945/5832262
ExecutorService fixedThreadPool Executors.newFixedThreadPool(3);Future future fixedThreadPool.submit(new Runnable() { Override public void run() {
new Runnable() {Overridepublic void run() {/** 确保线程不断执行不断刷新界面*/while (true(!Thread.currentThread().isInterrupted())) {try {Log.i(tag,线程运行中Thread.currentThread().getId());// 每执行一次暂停40毫秒//当sleep方法抛出InterruptedException 中断状态也会被清掉Thread.sleep(40);} catch (InterruptedException e) {e.printStackTrace();//如果抛出异常则再次设置中断请求Thread.currentThread().interrupt();}}}}
);
//触发条件设置中断
future.cancel(true);
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/90644.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!