当前位置: 首页 > news >正文

湖北什么网站建设值得推荐常州seo招聘

湖北什么网站建设值得推荐,常州seo招聘,企业微信小程序入口,贵阳网站建设哪家线程的中断 1.应用场景: 假设从网络下载一个100M的文件,如果网速很慢,用户等得不耐烦,就可能在下载过程中点“取消”,这时,程序就需要中断下载线程的执行。 2.常用中断线程的方法: 1.使用标…

线程的中断

1.应用场景:

假设从网络下载一个100M的文件,如果网速很慢,用户等得不耐烦,就可能在下载过程中点“取消”,这时,程序就需要中断下载线程的执行。

2.常用中断线程的方法:

1.使用标志位

2.interrupt()

1)如果当前线程sleep,wait或因为其他线程使用join()而陷入阻塞状态,那么当在其他线程中调用了该线程的interrupt()方法时,join()等方法会立刻抛出InterruptedException异常,目标线程只要捕获到该异常,会立刻结束运行
(2interrupt()不能中断I/O阻塞和synchronized锁阻塞
关于重置中断标志位:
isInterrupted()---不清除中断标志位,当前线程 public boolean isInterrupted() {//实例方法,不会重置标志位return isInterrupted(false);}
interrupted()-----清除中断标志位,又变成false,调用该方法的当前线程对象public static boolean interrupted() {//静态方法,会重置标志位return currentThread().isInterrupted(true);}

3.代码

3.1自己设置中断标志位

 class MyThreadTest extends Thread{
public  volatile boolean flag=true;
@Override
public void run() {//1.标志位
while(flag){System.out.println(Thread.currentThread().getName()+"是否中断---"+Thread.currentThread().isInterrupted()+"------"+Thread.currentThread().getState());
}
System.out.println("-----触发中断标志,中断了---");
}//测试的代码@Test
public void test1() throws InterruptedException {MyThreadTest t = new MyThreadTest("线程1");t.start();Thread.sleep(1);t.flag = false;
}

3.2调用 interrupt()

interrupted()

自定义线程测试
 class MyThread2 extends Thread {
@Override
public void run() {while (!isInterrupted()) {System.out.println(Thread.currentThread().getName() + "是否中断---" +Thread.interrupted() +"------" +Thread.currentThread().getState());}System.out.println(Thread.currentThread().getName() + " end "+Thread.interrupted());System.out.println(Thread.currentThread().getName() + " end "+Thread.interrupted());}}//测试的代码@Test
public void test2() throws InterruptedException {MyThread2 t2=new MyThread2();t2.start();Thread.sleep(1);t2.interrupt();
}

直接在主线程测试

public static void main(String[] args) throws InterruptedException {Thread.currentThread().interrupt();
System.out.println(Thread.interrupted());
System.out.println(Thread.interrupted());
}
}

isInterrupted()

自定义线程类

class MyThread3 extends Thread {@Overridepublic void run() {while (!isInterrupted()) {System.out.println(Thread.currentThread().getName() + "是否中断---" +Thread.currentThread().isInterrupted() +"------" +Thread.currentThread().getState());}System.out.println(Thread.currentThread().getName()+" end "+Thread.currentThread().isInterrupted());System.out.println(Thread.currentThread().getName()+" end "+Thread.currentThread().isInterrupted());}
}@Test
public void test3() throws InterruptedException {MyThread3 t3=new MyThread3();t3.start();Thread.sleep(1);t3.interrupt();
}

在主线程中测试

public static void main(String[] args) throws InterruptedException {Thread.currentThread().interrupt();System.out.println(Thread.currentThread().isInterrupted());System.out.println(Thread.currentThread().isInterrupted());
}
}

4.总结比较:

1.最好使用isInterrupted()方法进行中断标志的判断,因为不会重置标志位
2.对目标线程调用interrupt()方法可以请求中断一个线程,目标线程通过检测isInterrupted()标志获取自身是否已中断。
如果目标线程处于等待状态,该线程会捕获到InterruptedException;

3.目标线程检测到isInterrupted()为true或者捕获了InterruptedException都应该立刻结束自身线程;

4.通过标志位判断需要正确使用volatile关键字;

volatile关键字解决了共享变量在线程间的可见性问题。

http://www.hkea.cn/news/728824/

相关文章:

  • 如何做线上赌博的网站推广策略有哪些方法
  • 男的女的做那个视频网站百度收录需要多久
  • 大通县wap网站建设公司网站免费制作
  • 哪个网站教做公众号甘肃百度推广电话
  • 网站怎么让百度收录广告网络推广
  • 小型网站设计及建设论文定制网站制作公司
  • 视频网站建设费用排名优化网站seo排名
  • 怎么自己做网站服务器linux百度账号查询
  • 梧州网站推广方案百度热搜 百度指数
  • 网站不兼容ie6自助建站模板
  • 甘肃网站建设公司百中搜优化软件
  • 国内外贸网站建设公司seo教程 百度网盘
  • 一物一码二维码生成系统最好用的系统优化软件
  • 如何在大网站做外链镇江网站建站
  • 杭州网站建设公司导航短视频营销案例
  • 昆明做网站建设有哪些长尾关键词排名工具
  • 一女被多男做的视频网站网站seo系统
  • 网站建设 青海网站建设找哪家好
  • win7 网站配置优化方案官网电子版
  • 广州seo优化公司排名浙江seo博客
  • 全网推广的方式有哪些抖音seo推荐算法
  • 网站开发开源架构抖音营销软件
  • 自己做的网站能放到网上么青岛seo经理
  • 营业推广策划方案邵阳网站seo
  • 手机网站横向切换kol合作推广
  • 专门做超市海报的网站宁波seo咨询
  • 仿网站上的焦点图在线看seo网站
  • 做网站的业务员艾滋病阻断药有哪些
  • web集团网站建设广告投放平台有哪些
  • 大连做网站建设广告资源对接平台