java.lang.Thread.join(long millis, int nanos)%uA0方法等待至多为millis毫秒+毫微秒纳米秒该线程终止。
声明
以下是java.lang.Thread.join()方法的声明
public final void join(long millis, int nanos) throws InterruptedException
参数
-
millis%uA0-- 这是等待的毫秒数。
-
nanos%uA0-- 这是999999的附加纳秒等待时间。
返回值
此方法不返回任何值。
异常
-
IllegalArgumentException%uA0-- 如果millis的值是负的,毫微秒的值不在0-999999范围内。
-
InterruptedException%uA0-- 如果任何线程中断当前线程。当这种异常被抛出当前线程的中断状态被清除。
例子
下面的例子显示java.lang.Thread.join()方法的使用。
package com.yiibai 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