Java isAlive()和join()的使用

来源:互联网 发布:部落冲突防御设施数据 编辑:程序博客网 时间:2024/06/06 03:48
 Java isAlive()和join()的使用

如前所述,通常你希望主线程最后结束。在前面的例子中,这点是通过在main()中调用sleep()来实现的,经过足够长时间的延迟以确保所有子线程都先于主线程结束。然而,这不是一个令人满意的解决方法,它也带来一个大问题:一个线程如何知道另一线程已经结束?幸运的是,Thread类提供了回答此问题的方法。
有两种方法可以判定一个线程是否结束。第一,可以在线程中调用isAlive()。这种方法由Thread定义,它的通常形式如下:
final boolean isAlive( )
如果所调用线程仍在运行,isAlive()方法返回true,如果不是则返回false。但isAlive()很少用到,等待线程结束的更常用的方法是调用join(),描述如下:
final void join( ) throws InterruptedException
该方法等待所调用线程结束。该名字来自于要求线程等待直到指定线程参与的概念。join()的附加形式允许给等待指定线程结束定义一个最大时间。下面是前面例子的改进版本。运用join()以确保主线程最后结束。同样,它也演示了isAlive()方法。
/*@(#)NewThread.java   2017-4-17  * Copy Right 2017 Bank of Communications Co.Ltd. * All Copyright Reserved *//** * TODO Document NewThread * <p> * @version 1.0.0,2017-4-17 * @author Singit * @since 1.0.0 */public class NewThread implements Runnable{// Using join() to wait for threads to finish.    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: trueThread Two is alive: trueThread Three is alive: trueWaiting for threads to finish.One: 5Two: 5Three: 5One: 4Two: 4Three: 4One: 3Two: 3Three: 3One: 2Two: 2Three: 2One: 1Two: 1Three: 1Two exiting.Three exiting.One exiting.Thread One is alive: falseThread Two is alive: falseThread Three is alive: falseMain thread exiting.

如你所见,调用join()后返回,线程终止执行。
1 0
原创粉丝点击