最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2015年09月29日 15:34:13   投稿:goldensun  
這篇文章主要介紹了Java線程編程中isAlive()和join()的使用詳解,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下

一個(gè)線程如何知道另一線程已經(jīng)結(jié)束?Thread類提供了回答此問題的方法。

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

  final boolean isAlive( )

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

  final void join( ) throws InterruptedException

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

// 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.");
  }
}

程序輸出如下所示:

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.

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

相關(guān)文章

最新評(píng)論

读书| 岳普湖县| 吉木乃县| 化德县| 濮阳市| 新郑市| 闻喜县| 聂拉木县| 永新县| 宝山区| 日喀则市| 上栗县| 图木舒克市| 临沂市| 新密市| 中山市| 绥江县| 大兴区| 津南区| 阿巴嘎旗| 文山县| 长春市| 辽阳市| 新河县| 博客| 尼玛县| 南丹县| 安吉县| 墨竹工卡县| 修文县| 盱眙县| 都匀市| 拜泉县| 筠连县| 保定市| 洪洞县| 台北市| 大丰市| 汉川市| 迭部县| 英吉沙县|