一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java 并發(fā)編程之線程掛起、恢復(fù)與終止

Java 并發(fā)編程之線程掛起、恢復(fù)與終止

2020-09-24 15:55牛頭人 Java教程

這篇文章主要介紹了Java 并發(fā)編程之線程掛起、恢復(fù)與終止的相關(guān)資料,需要的朋友可以參考下

掛起恢復(fù)線程

    Thread 的API中包含兩個(gè)被淘汰的方法,它們用于臨時(shí)掛起和重啟某個(gè)線程,這些方法已經(jīng)被淘汰,因?yàn)樗鼈兪遣话踩模环€(wěn)定的。如果在不合適的時(shí)候掛起線程(比如,鎖定共享資源時(shí)),此時(shí)便可能會(huì)發(fā)生死鎖條件——其他線程在等待該線程釋放鎖,但該線程卻被掛起了,便會(huì)發(fā)生死鎖。另外,在長時(shí)間計(jì)算期間掛起線程也可能導(dǎo)致問題。

    下面的代碼演示了通過休眠來延緩運(yùn)行,模擬長時(shí)間運(yùn)行的情況,使線程更可能在不適當(dāng)?shù)臅r(shí)候被掛起:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public class DeprecatedSuspendResume extends Object implements Runnable{
  //volatile關(guān)鍵字,表示該變量可能在被一個(gè)線程使用的同時(shí),被另一個(gè)線程修改
 private volatile int firstVal;
 private volatile int secondVal;
 //判斷二者是否相等
 public boolean areValuesEqual(){
  return ( firstVal == secondVal);
 }
 public void run() {
  try{
   firstVal = 0;
   secondVal = 0;
   workMethod();
  }catch(InterruptedException x){
   System.out.println("interrupted while in workMethod()");
  }
 }
 private void workMethod() throws InterruptedException {
  int val = 1;
  while (true){
   stepOne(val);
   stepTwo(val);
   val++;
   Thread.sleep(200); //再次循環(huán)錢休眠200毫秒
  }
 }
 //賦值后,休眠300毫秒,從而使線程有機(jī)會(huì)在stepOne操作和stepTwo操作之間被掛起
 private void stepOne(int newVal) throws InterruptedException{
  firstVal = newVal;
  Thread.sleep(300); //模擬長時(shí)間運(yùn)行的情況
 }
 private void stepTwo(int newVal){
  secondVal = newVal;
 }
 public static void main(String[] args){
  DeprecatedSuspendResume dsr = new DeprecatedSuspendResume();
  Thread t = new Thread(dsr);
  t.start();
  //休眠1秒,讓其他線程有機(jī)會(huì)獲得執(zhí)行
  try {
   Thread.sleep(1000);}
  catch(InterruptedException x){}
  for (int i = 0; i < 10; i++){
   //掛起線程
   t.suspend();
   System.out.println("dsr.areValuesEqual()=" + dsr.areValuesEqual());
   //恢復(fù)線程
   t.resume();
   try{
    //線程隨機(jī)休眠0~2秒
    Thread.sleep((long)(Math.random()*2000.0));
   }catch(InterruptedException x){
    //略
   }
  }
  System.exit(0); //中斷應(yīng)用程序
 }
}

    某次運(yùn)行結(jié)果如下:

Java 并發(fā)編程之線程掛起、恢復(fù)與終止

    從areValuesEqual()返回的值有時(shí)為true,有時(shí)為false。以上代碼中,在設(shè)置firstVal之后,但在設(shè)置secondVal之前,掛起新線程會(huì)產(chǎn)生麻煩,此時(shí)輸出的結(jié)果會(huì)為false(情況1),這段時(shí)間不適宜掛起線程,但因?yàn)榫€程不能控制何時(shí)調(diào)用它的suspend方法,所以這種情況是不可避免的。

    當(dāng)然,即使線程不被掛起(注釋掉掛起和恢復(fù)線程的兩行代碼),如果在main線程中執(zhí)行asr.areValuesEqual()進(jìn)行比較時(shí),恰逢stepOne操作執(zhí)行完,而stepTwo操作還沒執(zhí)行,那么得到的結(jié)果同樣可能是false(情況2)。

     下面我們給出不用上述兩個(gè)方法來實(shí)現(xiàn)線程掛起和恢復(fù)的策略——設(shè)置標(biāo)志位。通過該方法實(shí)現(xiàn)線程的掛起和恢復(fù)有一個(gè)很好的地方,就是可以在線程的指定位置實(shí)現(xiàn)線程的掛起和恢復(fù),而不用擔(dān)心其不確定性。 

     對(duì)于上述代碼的改進(jìn)代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
public class AlternateSuspendResume extends Object implements Runnable {
 private volatile int firstVal;
 private volatile int secondVal;
 //增加標(biāo)志位,用來實(shí)現(xiàn)線程的掛起和恢復(fù)
 private volatile boolean suspended;
 public boolean areValuesEqual() {
  return ( firstVal == secondVal );
 }
 public void run() {
  try {
   suspended = false;
   firstVal = 0;
   secondVal = 0;
   workMethod();
  } catch ( InterruptedException x ) {
   System.out.println("interrupted while in workMethod()");
  }
 }
 private void workMethod() throws InterruptedException {
  int val = 1;
  while ( true ) {
   //僅當(dāng)賢臣掛起時(shí),才運(yùn)行這行代碼
   waitWhileSuspended();
   stepOne(val);
   stepTwo(val);
   val++;
   //僅當(dāng)線程掛起時(shí),才運(yùn)行這行代碼
   waitWhileSuspended();
   Thread.sleep(200);
  }
 }
 private void stepOne(int newVal)
     throws InterruptedException {
  firstVal = newVal;
  Thread.sleep(300);
 }
 private void stepTwo(int newVal) {
  secondVal = newVal;
 }
 public void suspendRequest() {
  suspended = true;
 }
 public void resumeRequest() {
  suspended = false;
 }
 private void waitWhileSuspended()
    throws InterruptedException {
  //這是一個(gè)“繁忙等待”技術(shù)的示例。
  //它是非等待條件改變的最佳途徑,因?yàn)樗鼤?huì)不斷請(qǐng)求處理器周期地執(zhí)行檢查,
  //更佳的技術(shù)是:使用Java的內(nèi)置“通知-等待”機(jī)制
  while ( suspended ) {
   Thread.sleep(200);
  }
 }
 public static void main(String[] args) {
  AlternateSuspendResume asr =
    new AlternateSuspendResume();
  Thread t = new Thread(asr);
  t.start();
  //休眠1秒,讓其他線程有機(jī)會(huì)獲得執(zhí)行
  try { Thread.sleep(1000); }
  catch ( InterruptedException x ) { }
  for ( int i = 0; i < 10; i++ ) {
   asr.suspendRequest();
   //讓線程有機(jī)會(huì)注意到掛起請(qǐng)求
   //注意:這里休眠時(shí)間一定要大于
   //stepOne操作對(duì)firstVal賦值后的休眠時(shí)間,即300ms,
   //目的是為了防止在執(zhí)行asr.areValuesEqual()進(jìn)行比較時(shí),
   //恰逢stepOne操作執(zhí)行完,而stepTwo操作還沒執(zhí)行
   try { Thread.sleep(350); }
   catch ( InterruptedException x ) { }
   System.out.println("dsr.areValuesEqual()=" +
     asr.areValuesEqual());
   asr.resumeRequest();
   try {
    //線程隨機(jī)休眠0~2秒
    Thread.sleep(
      ( long ) (Math.random() * 2000.0) );
   } catch ( InterruptedException x ) {
    //略
   }
  }
  System.exit(0); //退出應(yīng)用程序
 }
}

    運(yùn)行結(jié)果如下:

Java 并發(fā)編程之線程掛起、恢復(fù)與終止

   線程掛起的位置不確定main線程中執(zhí)行asr.areValuesEqual()進(jìn)行比較時(shí),恰逢stepOne操作執(zhí)行完,而stepTwo操作還沒執(zhí)行)asr.areValuesEqual()操作前,讓main線程休眠450ms(>300ms),如果掛起請(qǐng)求發(fā)出時(shí),新線程正執(zhí)行到或即將執(zhí)行到stepOne操作(如果在其前面的話,就會(huì)響應(yīng)掛起請(qǐng)求,從而掛起線程),那么在stepTwo操作執(zhí)行前,main線程的休眠還沒結(jié)束,從而main線程休眠結(jié)束后執(zhí)行asr.areValuesEqual()操作進(jìn)行比較時(shí),stepTwo操作已經(jīng)執(zhí)行完,因此也不會(huì)出現(xiàn)輸出結(jié)果為false的情況。

    可以將ars.suspendRequest()代碼后的sleep代碼去掉,或?qū)⑿菝邥r(shí)間改為200(明顯小于300即可)后,查看執(zhí)行結(jié)果,會(huì)發(fā)現(xiàn)結(jié)果中依然會(huì)有出現(xiàn)false的情況。如下圖所示:

Java 并發(fā)編程之線程掛起、恢復(fù)與終止

   總結(jié):線程的掛起和恢復(fù)實(shí)現(xiàn)的正確方法是:通過設(shè)置標(biāo)志位,讓線程在安全的位置掛起

終止線程

 終止線程的替代方法:同樣是使用標(biāo)志位,通過控制標(biāo)志位來終止線程。

以上所述是小編給大家介紹的Java 并發(fā)編程之線程掛起、恢復(fù)與終止,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!

原文鏈接:http://www.cnblogs.com/web424/p/6807319.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: a级毛片毛片免费很很综合 a级黄色视屏 | 国产1区精品 | 毛片大全高清免费 | 久9视频这里只有精品123 | 暖暖视频高清图片免费完整版 | 99久久国语露脸精品国产 | 亚洲网红精品大秀在线观看 | 草草免费观看视频在线 | 调教女警花穿环上班 | 亚洲大逼 | 国产午夜永久福利视频在线观看 | 九九久久国产精品大片 | 男女车车好快的车车免费网站 | 欧美免赞性视频 | 欧美男同video | 国产在线看片护士免费视频 | 亚洲天堂三区 | 亚洲视频在线观看地址 | 国产高清国内精品福利 | 亚洲成A人片在线观看中文L | 糖心视频在线观看 | 久久伊人久久 | 国产精品女同久久免费观看 | 精久久| 男生同性啪视频在线观看 | 日韩ab| 亚洲国产香蕉视频欧美 | 精品一区二区三区免费站 | 洗濯屋H纯肉动漫在线观看 武侠艳妇屈辱的张开双腿 午夜在线观看免费观看 视频 | 免费在线观看成年人视频 | 99re热这里只有精品 | 99最新网址| 午夜福利合集1000在线 | 精品国产无限资源免费观看 | 四虎影院com| 国产精品边做边接电话在线观看 | 欧美精品一国产成人性影视 | 九九国产在线视频 | 特大黑人娇小亚洲女mp4 | 人与蛇boxxⅹ | 边摸边吃奶边做爽gif动态图 |