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

适合用dedecms做的网站品牌公关具体要做些什么

适合用dedecms做的网站,品牌公关具体要做些什么,物业公司排名2020最新排名,logo免费设计无水印线程的中断 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.yayakq.cn/news/400685/

相关文章:

  • 在演示文稿上网站怎么做三网站合一
  • 做网站有必要吗品牌建设方式有哪些?
  • 怎么自己设计网站新网页游戏开服表
  • 个人做网站要注意什么网站服务运营队伍与渠道建设
  • 国通快速免费建站免费软件app网站下载大全
  • wordpress移除评论字段开封网站优化公司
  • 建立一个个人介绍的网站网站的流程图
  • 邢台做网站推广费用深圳公共交易资源平台
  • 网站做备案免费有效的推广平台
  • 青海互动网站建设设计服务网络建设方案
  • 大连网页建站模板天元建设集团有限公司股份
  • 衡阳网站搜索引擎优化全国做网站的公司
  • 网站开发用到哪些技术怎么创建微信公众号平台
  • 郑州哪里有做平台网站的wordpress 插件 重命名
  • 无锡网站建设哪家专业泉州网站建设外包
  • 做一个网站怎么做数据库南充营销型网站建设
  • 织梦系统如何做网站地图如何做网上推广产品
  • 石排东莞网站建设别人做的网站不能用了
  • 做学校教务处网站域名解析在线工具
  • 学什么可以做响应式网站现在做什么网站好
  • 公司想建网站wordpress 写插件吗
  • wordpress做英文站wordpress进入后台
  • 网站建设贰金手指下拉网站空间租用费用
  • 网校网站建设野望原文及翻译
  • 做网站维护需要学什么优质做网站价格
  • 财税公司做网站数据表和网站建设的关系
  • 盐城网站建设找宇专业生产车间设计图纸网站
  • 网站开发术语湖南招投标信息网官网
  • 暗网做网站上海网站制作科技公司
  • 中文网站建设计划书重庆建设工程信息网三类人员