Java 教程

Java 控制语句

面向对象编程

Java 内置类

Java 文件处理

Java 错误与异常

Java 多线程

Java 同步

Java 网络

Java 集合

Java 接口

Java 数据结构

Java 集合算法

高级 Java

Java 杂项

Java API 和框架

Java 类参考

Java 有用资源

Java - 线程连接



创建 Thread 对象后,可以通过调用 start() 方法 来启动它,该方法会执行对 run() 方法 的调用。在多个线程运行的情况下,我们可以阻塞当前线程,直到另一个线程终止。

Java 中的线程连接

Java 中的线程连接是指等待(或阻塞)一个线程,直到另一个线程完成其执行。 Threadjoin() 方法用于此目的。

语法

以下是 join() 方法的简单语法:

void join();

重载的 Thread.join() 方法

以下是三个重载的 join() 方法:

  • join() - 当前线程在第二个线程上调用此方法,导致当前线程阻塞,直到第二个线程终止。

  • join(long millisec) - 当前线程在第二个线程上调用此方法,导致当前线程阻塞,直到第二个线程终止或指定的毫秒数过去。

  • join(long millisec, int nanos) - 当前线程在第二个线程上调用此方法,导致当前线程阻塞,直到第二个线程终止或指定的毫秒数 + 纳秒数过去。

Java 中线程连接的示例

在此示例中,我们通过实现 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");
      // start t1 thread and join main thread
      t1.start();
      t1.join();	  
      // t2 will start when t1 is dead
      t2.start();        
      t2.join();
      // t3 will start when t2 is dead
      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

Java 中线程连接的更多示例

示例 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) {
            // TODO Auto-generated catch block
         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");
      // start t1 thread and join main thread
      t1.start();
      t1.join(200);	  
      // t2 will start when t1 is dead or 200 ms is elapsed
      t2.start();        
      t2.join(200);
      // t3 will start when t2 is dead or 200 ms is elapsed
      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) {
            // TODO Auto-generated catch block
         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");
      
      // start t1 thread and join main thread
      t1.start();
      t1.join(200,100000);	  
      
      // t2 will start when t1 is dead or 201 ms is elapsed
      t2.start();        
      t2.join(200,100000);
      
      // t3 will start when t2 is dead or 201 ms is elapsed
      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
广告