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

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - 詳解Java編程中線程的掛起、恢復和終止的方法

詳解Java編程中線程的掛起、恢復和終止的方法

2020-01-07 14:25goldensun JAVA教程

這篇文章主要介紹了詳解Java編程中線程的掛起、恢復和終止的方法,線程是Java學習中的重點和難點知識,需要的朋友可以參考下

有時,線程的掛起是很有用的。例如,一個獨立的線程可以用來顯示當日的時間。如果用戶不希望用時鐘,線程被掛起。在任何情形下,掛起線程是很簡單的,一旦掛起,重新啟動線程也是一件簡單的事。

掛起,終止和恢復線程機制在Java 2和早期版本中有所不同。盡管你運用Java 2的途徑編寫代碼,你仍需了解這些操作在早期Java環境下是如何完成的。例如,你也許需要更新或維護老的代碼。你也需要了解為什么Java 2會有這樣的變化。因為這些原因,下面內容描述了執行線程控制的原始方法,接著是Java 2的方法。
Java 1.1或更早版本的線程的掛起、恢復和終止

先于Java2的版本,程序用Thread 定義的suspend() 和 resume() 來暫停和再啟動線程。它們的形式如下:

?
1
2
final void suspend( )
final void resume( )


下面的程序描述了這些方法:

?
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
// Using suspend() and resume().
class NewThread implements Runnable {
  String name; // name of thread
  Thread t;
  NewThread(String threadname) {
    name = threadname;
    t = new Thread(this, name);
    System.out.println("New thread: " + t);
    t.start(); // Start the thread
  }
  // This is the entry point for thread.
  public void run() {
    try {
      for(int i = 15; i > 0; i--) {
        System.out.println(name + ": " + i);
        Thread.sleep(200);
      }
    } catch (InterruptedException e) {
      System.out.println(name + " interrupted.");
    }
    System.out.println(name + " exiting.");
  }
}
class SuspendResume {
  public static void main(String args[]) {
    NewThread ob1 = new NewThread("One");
    NewThread ob2 = new NewThread("Two");
    try {
      Thread.sleep(1000);
      ob1.t.suspend();
      System.out.println("Suspending thread One");
      Thread.sleep(1000);
      ob1.t.resume();
      System.out.println("Resuming thread One");
      ob2.t.suspend();
      System.out.println("Suspending thread Two");
      Thread.sleep(1000);
      ob2.t.resume();
      System.out.println("Resuming thread Two");
    } catch (InterruptedException e) {
      System.out.println("Main thread Interrupted");
    }
    // wait for threads to finish
    try {
      System.out.println("Waiting for threads to finish.");
      ob1.t.join();
      ob2.t.join();
    } catch (InterruptedException e) {
      System.out.println("Main thread Interrupted");
    }
    System.out.println("Main thread exiting.");
  }
}

程序的部分輸出如下:

?
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
New thread: Thread[One,5,main]
One: 15
New thread: Thread[Two,5,main]
Two: 15
One: 14
Two: 14
One: 13
Two: 13
One: 12
Two: 12
One: 11
Two: 11
Suspending thread One
Two: 10
Two: 9
Two: 8
Two: 7
Two: 6
Resuming thread One
Suspending thread Two
One: 10
One: 9
One: 8
One: 7
One: 6
Resuming thread Two
Waiting for threads to finish.
Two: 5
One: 5
Two: 4
One: 4
Two: 3
One: 3
Two: 2
One: 2
Two: 1
One: 1
Two exiting.
One exiting.
Main thread exiting.

Thread類同樣定義了stop() 來終止線程。它的形式如下:

?
1
void stop( )


一旦線程被終止,它不能被resume() 恢復繼續運行。
Java中掛起、恢復和終止線程

Thread定義的suspend(),resume()和stop()方法看起來是管理線程的完美的和方便的方法,它們不能用于新Java版本的程序。下面是其中的原因。Thread類的suspend()方法在Java2中不被贊成,因為suspend()有時會造成嚴重的系統故障。假定對關鍵的數據結構的一個線程被鎖定的情況,如果該線程在那里掛起,這些鎖定的線程并沒有放棄對資源的控制。其他的等待這些資源的線程可能死鎖。

Resume()方法同樣不被贊同。它不引起問題,但不能離開suspend()方法而獨立使用。Thread類的stop()方法同樣在Java 2中受到反對。這是因為該方法可能導致嚴重的系統故障。設想一個線程正在寫一個精密的重要的數據結構且僅完成一個零頭。如果該線程在此刻終止,則數據結構可能會停留在崩潰狀態。

因為在Java 2中不能使用suspend(),resume()和stop() 方法來控制線程,你也許會想那就沒有辦法來停止,恢復和結束線程。其實不然。相反,線程必須被設計以使run() 方法定期檢查以來判定線程是否應該被掛起,恢復或終止它自己的執行。有代表性的,這由建立一個指示線程狀態的標志變量來完成。只要該標志設為“running”,run()方法必須繼續讓線程執行。如果標志為“suspend”,線程必須暫停。若設為“stop”,線程必須終止。

當然,編寫這樣的代碼有很多方法,但中心主題對所有的程序應該是相同的。

下面的例題闡述了從Object繼承的wait()和notify()方法怎樣控制線程的執行。該例與前面講過的程序很像。然而,不被贊同的方法都沒有用到。讓我們思考程序的執行。

NewTread 類包含了用來控制線程執行的布爾型的實例變量suspendFlag。它被構造函數初始化為false。Run()方法包含一個監測suspendFlag 的同步聲明的塊。如果變量是true,wait()方法被調用以掛起線程。Mysuspend()方法設置suspendFlag為true。Myresume()方法設置suspendFlag為false并且調用notify()方法來喚起線程。最后,main()方法被修改以調用mysuspend()和myresume()方法。

?
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
// Suspending and resuming a thread for Java2
class NewThread implements Runnable {
  String name; // name of thread
  Thread t;
  boolean suspendFlag;
  NewThread(String threadname) {
    name = threadname;
    t = new Thread(this, name);
    System.out.println("New thread: " + t);
    suspendFlag = false;
    t.start(); // Start the thread
  }
  // This is the entry point for thread.
  public void run() {
    try {
      for(int i = 15; i > 0; i--) {
        System.out.println(name + ": " + i);
        Thread.sleep(200);
        synchronized(this) {
          while(suspendFlag) {
            wait();
          }
        }
      }
    } catch (InterruptedException e) {
      System.out.println(name + " interrupted.");
    }
    System.out.println(name + " exiting.");
  }
  void mysuspend() {
    suspendFlag = true;
  }
  synchronized void myresume() {
    suspendFlag = false;
    notify();
  }
}
class SuspendResume {
  public static void main(String args[]) {
    NewThread ob1 = new NewThread("One");
    NewThread ob2 = new NewThread("Two");
    try {
     Thread.sleep(1000);
     ob1.mysuspend();
     System.out.println("Suspending thread One");
     Thread.sleep(1000);
     ob1.myresume();
     System.out.println("Resuming thread One");
     ob2.mysuspend();
     System.out.println("Suspending thread Two");
     Thread.sleep(1000);
     ob2.myresume();
     System.out.println("Resuming thread Two");
    } catch (InterruptedException e) {
     System.out.println("Main thread Interrupted");
    }
    // wait for threads to finish
    try {
     System.out.println("Waiting for threads to finish.");
     ob1.t.join();
     ob2.t.join();
    } catch (InterruptedException e) {
      System.out.println("Main thread Interrupted");
    }
    System.out.println("Main thread exiting.");
  }
}

該程序的輸出與前面的程序相同。此書的后面部分,你將看到用Java 2機制控制線程的更多例子。盡管這種機制不像老方法那樣“干凈”,然而,它是確保運行時不發生錯誤的方法。它是所有新的代碼必須采用的方法。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99久久精品99999久久 | 日韩视频一区二区 | 日本免费全黄一级裸片视频 | a∨79成人网| 91免费高清无砖码区 | ts人妖国产一区 | 欧美久久久久久 | 亚洲冬月枫中文字幕在线看 | 性做久久久久久久久浪潮 | 国产成人精品午夜在线播放 | 成人国产午夜在线视频 | 999导航| 国产极品麻豆91在线 | 国产一卡2卡3卡四卡精品网 | 成全视频在线观看免费 | 亚洲国产日韩欧美在线vip1区 | 性一交一乱一伧老太 | 高清视频在线观看+免费 | 久久电影院久久国产 | 日韩欧美一区二区三区免费观看 | 亚洲国产第一区二区三区 | 国产亚洲精aa在线观看不卡 | 男人亚洲天堂 | 久久毛片网站 | 国产精品免费aⅴ片在线观看 | 黑人biglackon10十 | 免费超级乱淫视频播放性 | 欧美一级片在线视频 | 亚洲好骚综合 | 免费特黄一级欧美大片在线看 | 9999热视频| 国产免费资源 | 无人影院免费观看 | 日本免费一区二区三区 | 欧美日韩中文字幕久久伊人 | 亚洲欧美精品久久 | 99日影院在线播放 | 日本一道一区二区免费看 | 久草热在线| 日韩一区三区 | 美女自插 |