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

宾川网站建设企业展示型网站php

宾川网站建设,企业展示型网站php,做静态网站选用什么服务器,中国画廊企业网站模板线程的中断 1.应用场景#xff1a; 假设从网络下载一个100M的文件#xff0c;如果网速很慢#xff0c;用户等得不耐烦#xff0c;就可能在下载过程中点“取消”#xff0c;这时#xff0c;程序就需要中断下载线程的执行。 2.常用中断线程的方法#xff1a; 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 flagtrue; 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 t2new 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 t3new 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/14373374/

相关文章:

  • 一个好的网站应该具有什么吉林省建设工程安管人员管理系统
  • 济南正宗网站建设报价网站ico制作
  • 中小企业一站式服务平台三合一网站什么开发好
  • 网站打开显示站点目录做网站动态背景的图片
  • 宁夏住房和城乡建设厅网站福建省环保厅网站建设项目验收
  • 合肥网站建设设计外包百度云 做网站
  • 旅游网站建设ppt模板做外包网站的公司是怎样的
  • 宏大建设集团有限公司网站江华网站建设
  • 网站请人做要多少钱德化住房和城乡建设网站
  • 网站数据库一般多大如何做广告宣传与推广
  • dw做的网站后台是什么锡林郭勒盟建设厅官方网站
  • 摄影学校网站模板北京做网站的外包公司
  • led灯网站模板discuz论坛和网站同步登录
  • 深圳做网站的地方一个门户网站的建设流程
  • 做网站需要交维护费么地方门户网站推广方法有那些
  • wordpress 小众软件 主题百度优化推广
  • 网站备案接入服务单位做网站全程指导
  • 做学校网站素材图片大全一舍设计公司
  • 网站制作的一般步骤是什么便捷网站建设费用
  • 有哪些做企业网站的教学互动网站开发背景
  • 潍坊网站制作最低价格wordpress制作分类层级
  • 中国建设银行北京招聘信息网站河北seo网站设计
  • 网站空间多少钱东莞网站推广团队
  • 西安网站建设哪家专业网站软文推广网站
  • 企业网站开发前台模块设计网站代码模板免费
  • 10个网站做站群wordpress ckeditor
  • 怎么做微信小程序游戏wordpress网站怎么优化
  • 2016建设银行辽宁招聘网站重庆做网站建设的公司
  • 企业网站管理系统多站多语言版wordpress百度提交插件
  • 怎么制作个人求职网站北京正规网站建设比较