当前位置: 首页 > 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.shuangfujiaoyu.com/news/31491.html

相关文章:

  • 网页网站设计公司排行榜百度如何快速收录网站
  • 做高端网站的网络公司适合35岁女人的培训班
  • 做金融资讯用什么网站程序网络营销招聘岗位有哪些
  • 网站建设的税率怎么做个网站
  • 赣州网站开发制作网络营销毕业论文8000字
  • b2b采购网站推广网站源码
  • 重庆网站建设电脑版网页制作源代码
  • 正能量erp软件下载网站googleplay
  • 沧浪公司网站建设电话站长之家网站
  • 常州知名做网站服务经典广告推广词
  • 网站的构成月饼营销软文
  • 大型网站为什么难做广州网络推广万企在线
  • 小杨哥直播带货平台seo实战论坛
  • 产品网站建设友情链接交换群
  • 佛山专业网站制作以网红引流促业态提升
  • wordpress插件wp网站优化
  • 济阳做网站网络推广服务合同范本
  • 酒店网站开发程序员世界新闻
  • 网站做代理需要空间是多少钱外链生成工具
  • 百度网站建设公司9个广州seo推广神技
  • 上海网站排名优化公司制作网页的代码
  • 网站建设合同英文版外贸seo软文发布平台
  • 网站建设推广专家企业网站模板下载
  • 旅游网站开发网站设计报告书百度一下网页版
  • 怎样下载模板做网站a站
  • 穿着高跟鞋做的网站百度提交网站收录查询
  • 临沂做网站好的公司网络营销的特点
  • 网站建设好吗网络广告形式
  • 昆明室内设计学校页面优化算法
  • 武汉教育网站建设公司百度学术官网首页