龙空技术网

Java的join()方法

尚硅谷教育 148

前言:

此刻朋友们对“java join方法使用”大致比较着重,看官们都想要剖析一些“java join方法使用”的相关文章。那么小编同时在网摘上汇集了一些对于“java join方法使用””的相关内容,希望朋友们能喜欢,各位老铁们快快来学习一下吧!

Java的join()方法

Java中的join()方法由java.lang.Thread类提供,允许一个线程等待另一个线程执行完毕。假设th是Thread类的对象,其线程当前正在执行,那么th.join()语句确保在程序执行下一条语句之前th执行完毕。当有多个线程调用join()方法时,会对join()方法进行重载,允许开发人员指定等待的时间段。

然而,类似于Java中的sleep()方法,join()方法也依赖于操作系统的计时,因此我们不应该假设join()方法会等待我们在参数中指定的时间。以下是三种重载的join()方法。

重载的join()方法的描述

join(): 当调用join()方法时,当前线程停止执行并进入等待状态。当前线程将保持等待状态,直到调用join()方法的线程达到终止状态。如果线程被中断,则会抛出InterruptedException。

语法:

public final void join() throws InterruptedException

join(long mls): 当调用join()方法时,当前线程停止执行并进入等待状态。当前线程将保持等待状态,直到调用join()方法的线程死亡或等待的指定时间(以毫秒为单位)结束。

语法:

public final synchronized void join(long mls) throws InterruptedException, where mls is in milliseconds

join(long mls, int nanos): 当调用join()方法时,当前线程停止执行并进入等待状态。当前线程将保持等待状态,直到调用join()方法的线程死亡或等待的指定时间(以毫秒+纳秒为单位)结束。

语法:

public final synchronized void join(long mls, int nanos) throws InterruptedException, where mls is in milliseconds.

Java中join()方法的示例

下面的程序展示了join()方法的用法。

文件名:ThreadJoinExample.java

// 一个用于理解的Java程序

// 线程的加入

// 导入语句

import java.io.*;

// ThreadJoin 类是 Thread 类的子类

class ThreadJoin extends Thread

{

//覆盖运行方法

public void run()

{

for (int j = 0; j < 2; j++)

{

try

{

// 使线程休眠 300 毫秒

Thread.sleep(300);

System.out.println("The current thread name is: " + Thread.currentThread().getName());

}

//catch 块用于捕获引发的异常

catch(Exception e)

{

System.out.println("The exception has been caught: " + e);

}

System.out.println( j );

}

}

}

public class ThreadJoinExample

{

//主要方法

public static void main (String argvs[])

{

//创建3个线程

ThreadJoin th1 = new ThreadJoin();

ThreadJoin th2 = new ThreadJoin();

ThreadJoin th3 = new ThreadJoin();

//线程th1开始

th1.start();

//在什么时候开始第二个线程

//第一个线程 th1 已经结束或死亡。

try

{

System.out.println("The current thread name is: "+ Thread.currentThread().getName());

// 调用 join() 方法

th1.join();

}

// catch 块用于捕获引发的异常

catch(Exception e)

{

System.out.println("The exception has been caught " + e);

}

//线程th2开始

th2.start();

// 在线程 th2 结束或死亡后启动 th3 线程。

try

{

System.out.println("The current thread name is: " + Thread.currentThread().getName());

th2.join();

}

// catch 块用于捕获引发的异常

catch(Exception e)

{

System.out.println("The exception has been caught " + e);

}

//线程th3开始

th3.start();

}

}

输出:

The current thread name is: main

The current thread name is: Thread - 0

0

The current thread name is: Thread - 0

1

The current thread name is: main

The current thread name is: Thread - 1

0

The current thread name is: Thread - 1

1

The current thread name is: Thread - 2

0

The current thread name is: Thread - 2

1

解释:上面的程序展示了第二个线程th2在第一个线程th1结束后开始执行,并且线程th3在第二个线程th2结束或终止后开始执行。

join()方法:InterruptedException

我们在join()方法的描述中学到,当线程被中断时,会导致抛出InterruptedException异常。下面的示例展示了这一点。

文件名:ThreadJoinExample1.java

class ABC extends Thread

{

Thread threadToInterrupt;

// 覆盖 run() 方法

public void run()

{

// 调用方法中断

threadToInterrupt.interrupt();

}

}

public class ThreadJoinExample1

{

// 主要方法

public static void main(String[] argvs)

{

try

{

// 创建类 ABC 的对象

ABC th1 = new ABC();

th1.threadToInterrupt = Thread.currentThread();

th1.start();

// 调用 join() 方法导致

// 到 InterruptedException 的产生

th1.join();

}

catch (InterruptedException ex)

{

System.out.println("The exception has been caught. " + ex);

}

}

}

输出:

The exception has been caught. java.lang.InterruptedException

join()方法的更多示例

让我们看一些其他的例子。

文件名:TestJoinMethod1.java

class TestJoinMethod1 extends Thread{

public void run(){

for(int i=1;i<=5;i++){

try{

Thread.sleep(500);

}catch(Exception e){System.out.println(e);}

System.out.println(i);

}

}

public static void main(String args[]){

TestJoinMethod1 t1=new TestJoinMethod1();

TestJoinMethod1 t2=new TestJoinMethod1();

TestJoinMethod1 t3=new TestJoinMethod1();

t1.start();

try{

t1.join();

}catch(Exception e){System.out.println(e);}

t2.start();

t3.start();

}

}

输出:

1

2

3

4

5

1

1

2

2

3

3

4

4

5

5

我们可以看到在上面的例子中,当t1完成任务后,t2和t3开始执行。

join(long milliseconds)方法的示例

文件名:TestJoinMethod2.java

class TestJoinMethod2 extends Thread{

public void run(){

for(int i=1;i<=5;i++){

try{

Thread.sleep(500);

}catch(Exception e){System.out.println(e);}

System.out.println(i);

}

}

public static void main(String args[]){

TestJoinMethod2 t1=new TestJoinMethod2();

TestJoinMethod2 t2=new TestJoinMethod2();

TestJoinMethod2 t3=new TestJoinMethod2();

t1.start();

try{

t1.join(1500);

}catch(Exception e){System.out.println(e);}

t2.start();

t3.start();

}

}

输出:

1

2

3

1

4

1

2

5

2

3

3

4

4

5

5

在上面的示例中,当t1完成1500毫秒(3次)的任务后,t2和t3开始执行。

标签: #java join方法使用 #java中join方法