日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java stop和int,JAVA并发任务中止的isinterrupted,interupted()

發布時間:2024/9/19 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java stop和int,JAVA并发任务中止的isinterrupted,interupted() 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

將一個任務的不同步驟劃分成子任務放到不同的線程里,這樣實現的并行化并不是一個很好的辦法。這些異質的子任務之間有協作關系,需要有效的機制

同步——而額外的同步會對性能有一定影響,更重要的是會影響scalability。一個更好的辦法是將任務劃分成多個獨立的同質子任務,這樣子任務可以在不需要

同步的情況下并行執行,性能提升的效果更明顯,scalability也更強。

2.4. 任務的取消與終止

Java中沒有安全的終止一個線程的方法,因此必須采用協作的方式來實現。

There is no safe way to preemptively stop a thread in Java, and therefore no safe way to preemptively stop a task. There are only cooperative mechanisms, by which the task and the code requesting cancellation follow an agreed-upon protocol.

再抓InterruptedException的時候要三思,要想好該做些什么,因為:

Thread interruption is a cooperative mechanism for a thread to signal another thread that it should, at its convenience and if it feels like it, stop what it is doing and do something else.

isInterrupted方法返回線程當前的interrupted狀態;interrupted方法清除并返回interrupted狀態。

如果打斷發生時線程阻塞在sleep,wait之類的方法中,那它們會重置interrupted狀態并拋出InterruptedException。這意味著在抓InterruptedException的時候調用isInterrupted會返回false。

如果打斷發生在線程正常運行時,那打斷只會將interrupted標志改成true,線程需要自己檢查并清除這個標志。

如果在進入阻塞方法之前線程就已經被打斷并且interrupted標志未被重置,那這些阻塞方法會立刻拋出InterruptedException。見如下例子:

這個程序的輸出:

Because each thread has its own interruption policy, you should not interrupt a thread unless you know what interruption means to that thread.

總結

以上是生活随笔為你收集整理的java stop和int,JAVA并发任务中止的isinterrupted,interupted()的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。