void join(long millis, int nanos)
描述 (Description)
java.lang.Thread.join(long millis, int nanos)方法等待此线程死亡时最多毫秒加上纳秒纳秒。
声明 (Declaration)
以下是java.lang.Thread.join()方法的声明
public final void join(long millis, int nanos) throws InterruptedException
参数 (Parameters)
millis - 这是等待的时间,以毫秒为单位。
nanos - 等待的额外纳秒为999999纳秒。
返回值 (Return Value)
此方法不返回任何值。
异常 (Exception)
IllegalArgumentException - 如果millis的值为负,则nanos的值不在0-999999范围内。
InterruptedException - 如果有任何线程中断了当前线程。 抛出此异常时,将清除当前线程的中断状态。
例子 (Example)
以下示例显示了java.lang.Thread.join()方法的用法。
package com.iowiki;
import java.lang.*;
public class ThreadDemo implements Runnable {
public void run() {
Thread t = Thread.currentThread();
System.out.print(t.getName());
//checks if this thread is alive
System.out.println(", status = " + t.isAlive());
}
public static void main(String args[]) throws Exception {
Thread t = new Thread(new ThreadDemo());
// this will call run() function
t.start();
/* waits at most 2000 milliseconds plus 500 nanoseconds for
this thread to die */
t.join(2000, 500);
System.out.println("after waiting for 2000 milliseconds
plus 500 nanoseconds ...");
System.out.print(t.getName());
//checks if this thread is alive
System.out.println(", status = " + t.isAlive());
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
Thread-0, status = true
after waiting for 2000 milliseconds plus 500 nanoseconds ...
Thread-0, status = false