创建 Thread 对象后,您可以通过调用 start() 方法来启动它,该方法会执行对 run( ) 的调用方法。当多个线程运行时,我们可以阻塞当前线程,直到另一个线程终止。
以下是 join() 方法的简单语法
语法
void join();
以下是各种版本的 join() 方法的详细信息。
join() - 当前线程在第二个线程上调用此方法,导致当前线程阻塞,直到第二个线程终止。
join(long millisec) - 当前线程在第二个线程上调用此方法,导致当前线程阻塞,直到第二个线程终止或经过指定的毫秒数。
join(long millisec, int nanos) - 当前线程在第二个线程上调用此方法,导致当前线程阻塞,直到第二个线程终止或经过指定的毫秒数 + 纳秒。
示例 1
在此示例中,我们通过实现 Runnable 接口创建一个 RunnableDemo 类。 RunnableDemo 类具有 run() 方法实现。在主类 TestThread 中,我们创建了 RunnableDemo 对象,并使用这些对象创建了两个 Thread 对象。当对每个线程对象调用 Thread.start() 方法时,线程开始处理并执行程序。使用 join() 方法,我们阻塞当前线程,确保一旦线程完成,只有下一个线程才会启动。
package com.yxjc123;
class RunnableDemo implements Runnable {
RunnableDemo( ) {
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + "State: New");
}
public void run() {
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + "State: Running");
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + i);
}
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + "State: Dead");
}
}
public class TestThread {
public static void main(String args[]) throws InterruptedException {
Thread t1 = new Thread( new RunnableDemo(), "Thread-1");
Thread t2 = new Thread( new RunnableDemo(), "Thread-2");
Thread t3 = new Thread( new RunnableDemo(), "Thread-3");
//启动t1线程并加入主线程
t1.start();
t1.join();
//当t1死亡时t2将启动
t2.start();
t2.join();
//t3将在t2死亡时启动
t3.start();
}
}
输出
Thread: Thread-1, State: New
Thread: Thread-2, State: New
Thread: Thread-1, State: Running
Thread: Thread-1, 4
Thread: Thread-1, 3
Thread: Thread-1, 2
Thread: Thread-1, 1
Thread: Thread-1, State: Dead
Thread: Thread-2, State: Running
Thread: Thread-2, 4
Thread: Thread-2, 3
Thread: Thread-2, 2
Thread: Thread-2, 1
Thread: Thread-2, State: Dead
示例 2
在此示例中,我们通过实现 Runnable 接口创建一个 RunnableDemo 类。 RunnableDemo 类具有 run() 方法实现。在主类 TestThread 中,我们创建了 RunnableDemo 对象,并使用这些对象创建了两个 Thread 对象。当对每个线程对象调用 Thread.start() 方法时,线程开始处理并执行程序。使用 join(long millisec) 方法,我们将当前线程阻塞 200 毫秒,这确保一旦线程完成或发生 200 毫秒的延迟,则只有下一个线程才会启动。
package com.yxjc123;
class RunnableDemo implements Runnable {
RunnableDemo( ) {
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + "State: New");
}
public void run() {
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + "State: Running");
for(int i = 4; i > 0; i--) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + i);
}
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + "State: Dead");
}
}
public class TestThread {
public static void main(String args[]) throws InterruptedException {
Thread t1 = new Thread( new RunnableDemo(), "Thread-1");
Thread t2 = new Thread( new RunnableDemo(), "Thread-2");
Thread t3 = new Thread( new RunnableDemo(), "Thread-3");
//启动t1线程并加入主线程
t1.start();
t1.join(200);
// t2 将在 t1 死亡或经过 200 ms 时启动
t2.start();
t2.join(200);
//当t2死亡或经过200毫秒时,t3将启动
t3.start();
}
}
输出
Thread: main, State: New
Thread: main, State: New
Thread: main, State: New
Thread: Thread-1, State: Running
Thread: Thread-1, 4
Thread: Thread-1, 3
Thread: Thread-1, 2
Thread: Thread-2, State: Running
Thread: Thread-1, 1
Thread: Thread-1, State: Dead
Thread: Thread-2, 4
Thread: Thread-2, 3
Thread: Thread-2, 2
Thread: Thread-3, State: Running
Thread: Thread-2, 1
Thread: Thread-2, State: Dead
Thread: Thread-3, 4
Thread: Thread-3, 3
Thread: Thread-3, 2
Thread: Thread-3, 1
Thread: Thread-3, State: Dead
示例 3
在这个示例中,我们通过实现 Runnable 接口来创建一个 RunnableDemo 类。 RunnableDemo 类具有 run() 方法实现。在主类 TestThread 中,我们创建了 RunnableDemo 对象,并使用这些对象创建了两个 Thread 对象。当对每个线程对象调用 Thread.start() 方法时,线程开始处理并执行程序。使用 join(long millisec, long nanoseconds) 方法,我们将当前线程阻塞 200 毫秒和 100000 纳秒,这确保一旦线程完成或发生 201 毫秒的延迟,则只有下一个线程将启动。
package com.yxjc123;
class RunnableDemo implements Runnable {
RunnableDemo( ) {
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + "State: New");
}
public void run() {
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + "State: Running");
for(int i = 4; i > 0; i--) {
try {
Thread.sleep(49);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + i);
}
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + "State: Dead");
}
}
public class TestThread {
public static void main(String args[]) throws InterruptedException {
Thread t1 = new Thread( new RunnableDemo(), "Thread-1");
Thread t2 = new Thread( new RunnableDemo(), "Thread-2");
Thread t3 = new Thread( new RunnableDemo(), "Thread-3");
//启动t1线程并加入主线程
t1.start();
t1.join(200,100000);
// t2 将在 t1 死亡或经过 201 ms 时启动
t2.start();
t2.join(200,100000);
// t3 将在 t2 死亡或经过 201 ms 时启动
t3.start();
}
}
输出
Thread: main, State: New
Thread: main, State: New
Thread: main, State: New
Thread: Thread-1, State: Running
Thread: Thread-1, 4
Thread: Thread-1, 3
Thread: Thread-1, 2
Thread: Thread-1, 1
Thread: Thread-1, State: Dead
Thread: Thread-2, State: Running
Thread: Thread-2, 4
Thread: Thread-2, 3
Thread: Thread-2, 2
Thread: Thread-2, 1
Thread: Thread-2, State: Dead
Thread: Thread-3, State: Running
Thread: Thread-3, 4
Thread: Thread-3, 3
Thread: Thread-3, 2
Thread: Thread-3, 1
Thread: Thread-3, State: Dead