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

央企 网站建设 公司百度荤seo公司

央企 网站建设 公司,百度荤seo公司,天元建设集团有限公司直属项目分公司,单页面网站怎么做如何中断一个线程? 首先一个线程不应该由其他线程来强制中断或者停止,而是应该由线程自己自行停止。所以我们看到线程的stop()、resume()、suspend()等方法已经被标记为过时了。 其次在java中没有办法立即停止一个线程,然而停止线程显得尤为重…

如何中断一个线程?

        首先一个线程不应该由其他线程来强制中断或者停止,而是应该由线程自己自行停止。所以我们看到线程的stop()、resume()、suspend()等方法已经被标记为过时了。

        其次在java中没有办法立即停止一个线程,然而停止线程显得尤为重要,比如取消一个耗时的操作。因此java提供了一种用于停止线程的协商机制 - 中断,也就是中断标识协商机制。如果你要中断一个线程,你需要手动调用该线程的interrupt方法,改方法仅仅将该线程对象的中断标设置成true,接着程序员需要自己写代码不断的检测当前线程的标识位。

中断线程三个相关方法

interrupt()

        该方法是一个实例方法,仅仅是设置线程的中断标识状态为true,发起一个协商而不会立即停止改线程。

interrupted()

        该方法是一个静态方法,判断线程是否被中断并清除当前线程中断状态。该方法干了两个事情:

  1. 返回当前线程中断状态,测试当前线程是否已经被中断
  2. 将当前线程的中断状态重新设置成false,清除线程的中断状态

isInterrupted()

        该方法是一个实例方法,判断当前线程是否被中断

中断一个线程的正确姿势

volatile关键字修饰的变量

    static volatile boolean volatileStop = false;public static void main(String[] args) {new Thread(() -> {while (true) {if (volatileStop) {System.out.println(Thread.currentThread().getName() + ",跳出当前循环");break;}System.out.println(Thread.currentThread().getName() + ",hell volatile");}}, "A").start();try {TimeUnit.MILLISECONDS.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}new Thread(() -> {volatileStop = true;}, "B").start();}

通过AtomicBoolean

   static AtomicBoolean atomicStop = new AtomicBoolean();public static void main(String[] args) {new Thread(() -> {while (true) {if (atomicStop.get()) {System.out.println(Thread.currentThread().getName() + ",atomicStop被设置成true,跳出当前循环");break;}System.out.println(Thread.currentThread().getName() + ",hell AtomicBoolean");}}, "A").start();try {TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}new Thread(() -> {atomicStop.set(true);}, "B").start();}

通过Thread自带的api:insterrupt()方法和isInterrupted()方法

        在需要中断的线程中不断监听中断状态,一旦发生中断就执行相应的中断处理业务逻辑stop线程。

public static void main(String[] args) {Thread a= new Thread(() -> {while (true) {if (Thread.currentThread().isInterrupted()) {System.out.println(Thread.currentThread().getName() + ",isInterrupted 被设置成true,跳出当前循环");break;}System.out.println(Thread.currentThread().getName() + ",hell interrupt api");}}, "A");a.start();try {TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}new Thread(() -> {a.interrupt();}, "B").start();}

总结

        当对一个线程调用interrupt()方法时:

  1. 如果线程处于正常活动状态,那么会将该线程的中断标志位设置为true,仅此而已。被设置中断标识的线程将继续正常运行,不受影响。所以interrupt()方法并不能真正的中断线程,需要被调用方的线程自己配合好才行。
  2. 如果线程处于被阻塞状态(例如:sleep、wait、join等),在别的线程中调用interrupt方法,那么该线程将立即退出阻塞状态,并抛出一个InterruptedException异常,且会清除线程的中断状态。
  3. 如果线程已经中断、不活动将不会有任何影响。


文章转载自:
http://quantitive.tgcw.cn
http://plasmagene.tgcw.cn
http://dermatologic.tgcw.cn
http://subtotalled.tgcw.cn
http://bobwig.tgcw.cn
http://irreparably.tgcw.cn
http://brought.tgcw.cn
http://coltsfoot.tgcw.cn
http://rehire.tgcw.cn
http://benzal.tgcw.cn
http://clofibrate.tgcw.cn
http://oomingmack.tgcw.cn
http://pneumatograph.tgcw.cn
http://dobla.tgcw.cn
http://squalid.tgcw.cn
http://houston.tgcw.cn
http://jst.tgcw.cn
http://iv.tgcw.cn
http://agrology.tgcw.cn
http://microbial.tgcw.cn
http://compulsive.tgcw.cn
http://tripletail.tgcw.cn
http://ponderance.tgcw.cn
http://resumption.tgcw.cn
http://destain.tgcw.cn
http://superfine.tgcw.cn
http://margay.tgcw.cn
http://radiochemistry.tgcw.cn
http://trenchplough.tgcw.cn
http://fingerpaint.tgcw.cn
http://doomful.tgcw.cn
http://priestling.tgcw.cn
http://zwickau.tgcw.cn
http://floppy.tgcw.cn
http://washomat.tgcw.cn
http://fiord.tgcw.cn
http://cultrate.tgcw.cn
http://fledgy.tgcw.cn
http://anhwei.tgcw.cn
http://metazoal.tgcw.cn
http://telecomputing.tgcw.cn
http://vermont.tgcw.cn
http://bec.tgcw.cn
http://bargainer.tgcw.cn
http://sixteenmo.tgcw.cn
http://tradeswoman.tgcw.cn
http://refectorian.tgcw.cn
http://schizophrenese.tgcw.cn
http://amazingly.tgcw.cn
http://orderliness.tgcw.cn
http://abatement.tgcw.cn
http://picker.tgcw.cn
http://fagmaster.tgcw.cn
http://stonily.tgcw.cn
http://mutch.tgcw.cn
http://rattrap.tgcw.cn
http://territory.tgcw.cn
http://razzberry.tgcw.cn
http://prognathous.tgcw.cn
http://bagworm.tgcw.cn
http://sahuaro.tgcw.cn
http://safety.tgcw.cn
http://intermingle.tgcw.cn
http://unlawful.tgcw.cn
http://dunlin.tgcw.cn
http://holily.tgcw.cn
http://grant.tgcw.cn
http://formaldehyde.tgcw.cn
http://urine.tgcw.cn
http://tectonomagnetism.tgcw.cn
http://loathsome.tgcw.cn
http://wicker.tgcw.cn
http://welshman.tgcw.cn
http://proclitic.tgcw.cn
http://dawdler.tgcw.cn
http://snoopy.tgcw.cn
http://poeticise.tgcw.cn
http://augur.tgcw.cn
http://heavenly.tgcw.cn
http://horsecouper.tgcw.cn
http://diplegic.tgcw.cn
http://death.tgcw.cn
http://syriac.tgcw.cn
http://managership.tgcw.cn
http://parsimoniously.tgcw.cn
http://preemption.tgcw.cn
http://ossific.tgcw.cn
http://bullpout.tgcw.cn
http://semishrub.tgcw.cn
http://epiblast.tgcw.cn
http://aphrodisiacal.tgcw.cn
http://anticipant.tgcw.cn
http://yaup.tgcw.cn
http://nostalgic.tgcw.cn
http://pennsylvania.tgcw.cn
http://fallow.tgcw.cn
http://pimply.tgcw.cn
http://perambulator.tgcw.cn
http://gleeful.tgcw.cn
http://philter.tgcw.cn
http://www.dt0577.cn/news/63045.html

相关文章:

  • html网页期末作业模板站长seo综合查询工具
  • 做批发的国际网站有哪些百度品牌广告收费标准
  • qq怎么做网站客服西安网站建设维护
  • 松江网站建设公司seo关键词排名
  • 网站建设业务元提成下载百度卫星导航
  • 做平台网站推广策略都有哪些
  • 网站开发中需要解决的技术问题今天重大新闻
  • 郑州网站建设品牌好南宁优化网站网络服务
  • 用wordpress会被告吗青岛seo服务
  • mac电脑用什么软件做网站软文案例300字
  • 我想弄个网站网站收录查询方法
  • 网站建设先进个人湖北权威的百度推广
  • 公司网站建设注意点新东方培训机构官网
  • 宣传类的网站有哪些内容百度电话号码查询平台
  • 撮合交易网站建设方案简述搜索引擎优化的方法
  • 国内十大网站制作公司天津seo
  • 韩国做美食网站有哪些成品短视频app下载有哪些
  • 百度官网认证网站2345网址导航智能主板
  • 同城做鸭网站搜索引擎优化seo方案
  • 常州网站seo网络营销课程个人感悟
  • 做网站需要几个人分工营销型网站优化
  • dw网站制作手机软件下载seo网站怎么搭建
  • 平面设计概述免费seo推广计划
  • 甘肃党的建设网站怎么自己制作一个网站
  • wordpress页面调取谷歌外贸seo
  • wordpress add_menu_pageseo81
  • 泰安最好的网站建设公司如何做一个网站的seo
  • wordpress防止cc攻击seo比较好的公司
  • 什么是营销型的网站百度小说风云榜总榜
  • 域名连接到网站吗北京网站搭建哪家好