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

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

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

服務器之家 - 編程語言 - JAVA教程 - Java線程編程中isAlive()和join()的使用詳解

Java線程編程中isAlive()和join()的使用詳解

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

這篇文章主要介紹了Java線程編程中isAlive()和join()的使用詳解,是Java入門學習中的基礎知識,需要的朋友可以參考下

一個線程如何知道另一線程已經結束?Thread類提供了回答此問題的方法。

有兩種方法可以判定一個線程是否結束。第一,可以在線程中調用isAlive()。這種方法由Thread定義,它的通常形式如下:

?
1
final boolean isAlive( )

如果所調用線程仍在運行,isAlive()方法返回true,如果不是則返回false。但isAlive()很少用到,等待線程結束的更常用的方法是調用join(),描述如下:

?
1
final void join( ) throws InterruptedException

該方法等待所調用線程結束。該名字來自于要求線程等待直到指定線程參與的概念。join()的附加形式允許給等待指定線程結束定義一個最大時間。下面是前面例子的改進版本。運用join()以確保主線程最后結束。同樣,它也演示了isAlive()方法。

?
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
// Using join() to wait for threads to finish.
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 = 5; i > 0; i--) {
        System.out.println(name + ": " + i);
        Thread.sleep(1000);
      }
    } catch (InterruptedException e) {
      System.out.println(name + " interrupted.");
    }
    System.out.println(name + " exiting.");
  }
}
 
class DemoJoin {
  public static void main(String args[]) {
    NewThread ob1 = new NewThread("One");
    NewThread ob2 = new NewThread("Two");
    NewThread ob3 = new NewThread("Three");
    System.out.println("Thread One is alive: "+ ob1.t.isAlive());
    System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
    System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
    // wait for threads to finish
    try {
      System.out.println("Waiting for threads to finish.");
      ob1.t.join();
      ob2.t.join();
      ob3.t.join();
    } catch (InterruptedException e) {
      System.out.println("Main thread Interrupted");
    }
    System.out.println("Thread One is alive: "+ ob1.t.isAlive());
    System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
    System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
    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
New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
New thread: Thread[Three,5,main]
Thread One is alive: true
Thread Two is alive: true
Thread Three is alive: true
Waiting for threads to finish.
One: 5
Two: 5
Three: 5
One: 4
Two: 4
Three: 4
One: 3
Two: 3
Three: 3
One: 2
Two: 2
Three: 2
One: 1
Two: 1
Three: 1
Two exiting.
Three exiting.
One exiting.
Thread One is alive: false
Thread Two is alive: false
Thread Three is alive: false
Main thread exiting.

如你所見,調用join()后返回,線程終止執行。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 性free非洲老妇 | 久久亚洲午夜牛牛影视 | 爽好舒服把腿张小说 | 国产目拍亚洲精品一区二区三区 | 91精品国产91热久久p | 天天翘 | 91久久国产青草亚洲 | 国产3p在线| 日韩一级精品视频在线观看 | 99re最新网址 | 男女做性视频 | 女被男啪到哭 | 久久永久免费视频 | 十八女下面流水不遮免费 | 亚久久伊人精品青青草原2020 | 掰开逼操 | 男人女人日批 | 亚洲高清免费在线观看 | 国产目拍亚洲精品一区二区三区 | 九九精品视频在线播放 | 久久婷婷五月综合色精品首页 | 国产乱码一卡二卡3卡四卡 国产乱插 | 免费国产白棉袜踩踏区域 | 欧美精选视频 | 亚洲成aⅴ人片在线 | 好大好硬好长好爽a网站 | 羞羞影院午夜男女爽爽影院网站 | 亚洲视频99 | 成人国产在线视频 | 美女扒开肌肌让男人桶 | 美女扒开腿让男人桶爽动态图片 | 成人影院vs一区二区 | 免费一级国产生活片 | 国产成人精品视频一区二区不卡 | 亚洲人成综合在线播放 | 天天综合天天影视色香欲俱全 | 王淑兰李思雨李铁柱乡村小说免费 | 4438全国最大免费观看 | 免费视屏 | julianann在厨房| 亚洲不卡视频在线 |