一旦创建了一个Thread对象,你可以通过调用start()方法来启动它,这会导致执行run()方法。在多个线程运行的情况下,我们可以阻塞当前线程直到另一个线程终止。
在Java中加入线程
在Java中加入线程是指等待(或阻塞)一个线程直到另一个线程完成其执行。Thread类中的join()方法用于此目的。
加入线程的语法
以下是join()方法的简单语法:
void join();
重载的Thread.join()方法
以下是三个重载的join()方法:
-
join()
− 当前线程调用此方法在一个第二线程上,导致当前线程阻塞直到第二个线程终止。
-
join(long millisec)
− 当前线程调用此方法在一个第二线程上,导致当前线程阻塞直到第二个线程终止或指定的毫秒数过去。
-
join(long millisec, int nanos)
− 当前线程调用此方法在一个第二线程上,导致当前线程阻塞直到第二个线程终止或指定的毫秒数加上纳秒数过去。
加入线程的例子
在这个例子中,我们创建了一个通过实现Runnable接口的类RunnableDemo。RunnableDemo类包含了run()方法的实现。在主类TestThread中,我们创建了RunnableDemo的对象,并使用这些对象创建了两个Thread对象。当在每个线程对象上调用Thread.start()方法时,线程开始处理并且程序被执行。使用join()方法,我们阻塞了当前线程,确保一旦线程完成之后下一个线程才会开始。
示例代码
package com.tutorialspoint;
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.start();
t1.join();
t2.start();
t2.join();
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
更多加入线程的例子
示例1
在这个例子中,我们创建了一个通过实现Runnable接口的类RunnableDemo。RunnableDemo类包含了run()方法的实现。在主类TestThread中,我们创建了RunnableDemo的对象,并使用这些对象创建了两个Thread对象。当在每个线程对象上调用Thread.start()方法时,线程开始处理并且程序被执行。使用join(long millisec)方法,我们阻塞当前线程200毫秒,这确保一旦线程完成或延迟了200毫秒后,下一个线程才会开始。
示例代码
package com.tutorialspoint;
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) {
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.start();
t1.join(200);
t2.start();
t2.join(200);
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
示例2
在这个例子中,我们创建了一个通过实现Runnable接口的类RunnableDemo。RunnableDemo类包含了run()方法的实现。在主类TestThread中,我们创建了RunnableDemo的对象,并使用这些对象创建了两个Thread对象。当在每个线程对象上调用Thread.start()方法时,线程开始处理并且程序被执行。使用join(long millisec, long nanoseconds)方法,我们阻塞当前线程200毫秒加上100000纳秒,这确保一旦线程完成或延迟了201毫秒后,下一个线程才会开始。
示例代码
package com.tutorialspoint;
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) {
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.start();
t1.join(200,100000);
t2.start();
t2.join(200,100000);
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