线程类最终void join() (Thread Class final void join())

  • This method is available in package java.lang.Thread.join().

    软件包java.lang.Thread.join()中提供了此方法。

  • join() method is applicable when a thread wants to wait until completing some other thread then we should go for join() method of Thread class.

    join()方法 ,当一个线程要等到完成一些其他线程那么我们就应该去参加()Thread类的方法,方法是适用的。

  • This method is not static so we cannot access this method with the class name too.

    此方法不是静态的,因此我们也无法使用类名访问此方法。

  • This method is final we can't override this method in child class.

    此方法是最终方法,我们不能在子类中覆盖此方法。

  • The return type of this method is void so it does not return anything.

    此方法的返回类型为void,因此它不返回任何内容。

  • This method throws an InterruptedException so it is needed to handle exception either by try-catch or throws otherwise we will get a compile-time error.

    该方法抛出InterruptedException异常,因此需要通过try-catch或throws来处理异常,否则我们将获得编译时错误。

For example: We have three threads [t1 – PreparedExamPaper ] ,[t2 – PrintingExamPaper],[t3- DistributingExamPaper] so will see what will happen.

例如:我们有三个线程[ t1 – PreparedExamPaper],[ t2 – PrintingExamPaper],[ t3 -DistributingExamPaper],因此将看到会发生什么。

Let suppose if a thread t1 executes, t2.join(), then thread t1 will entered into waiting for the state until t2 completes once t2 completes then t1 will continue its execution.

假设如果线程t1执行t2.join() ,则线程t1将进入等待状态,直到t2完成,一旦t2完成,则t1将继续执行。

Similarly, If a thread t2 executes, t3.join() then thread t2 will entered into waiting for the state until t3 completes once t3 completes, then t2 will continue its execution.

同样,如果线程t2执行t3.join(),则线程t2将进入等待状态,直到t3完成后t3完成,然后t2将继续执行。

Syntax:

句法:

    final void join(){
}

Parameter(s):

参数:

When we write t2.join(), so this line means currently executing thread will stop its execution and that thread will wait() for t2 completion.

当我们编写t2.join()时 ,此行表示当前正在执行的线程将停止执行,并且该线程将等待( 2)完成t2 。

Return value:

返回值:

The return type of this method is void, it does not return anything.

此方法的返回类型为void ,它不返回任何内容。

Java程序演示join()方法的示例 (Java program to demonstrate example of join() method)

/*  We will use Thread class methods so we are importing
the package but it is not mandated because
it is imported by default
*/
import java.lang.Thread;
class MyThread extends Thread {//Override run() method of Thread class
public void run() {for (int i = 0; i < 5; ++i)
System.out.println("We are in MyThread");
try {Thread.sleep(500);
} catch (Exception ex) {System.out.println(ex.getMessage());
}
}
}
class MainThread {public static void main(String[] args) throws Exception {MyThread mt = new MyThread();
mt.start();
/* Note -1*/
mt.join();
for (int j = 0; j < 5; ++j)
System.out.println("We are in MainThread");
}
}

Note 1: If we comment /*mt.join()*/ in the above program then both threads will execute simultaneously we can't expect exact execution order and then we can't expect exact output.

注1:如果在上面的程序中注释/*mt.join()*/,那么两个线程将同时执行,我们不能期望确切的执行顺序,也不能期望精确的输出。

Output

输出量

E:\Programs>javac MainThread.java
E:\Programs>java MainThread
We are in MyThread
We are in MyThread
We are in MyThread
We are in MyThread
We are in MyThread
We are in MainThread
We are in MainThread
We are in MainThread
We are in MainThread
We are in MainThread

翻译自: https://www.includehelp.com/java/thread-class-final-void-join-method-with-example.aspx

Java Thread类的最终void join()方法与示例相关推荐

  1. setpriority_Java Thread类的最终void setPriority(int priority)方法(带示例)

    setpriority 线程类最终void setPriority(int priority) (Thread Class final void setPriority(int priority)) ...

  2. Java Thread类的静态void sleep(long time_in_ms)方法,带示例

    线程类静态无效睡眠(long time_in_ms) (Thread Class static void sleep(long time_in_ms)) This method is availabl ...

  3. getpriority java_Java Thread类的最终int getPriority()方法(带示例)

    线程类final int getPriority()包java.lang.Thread.getPriority()中提供了此方法. 此方法用于返回此线程的优先级. 此方法不是静态的,因此该方法可通过T ...

  4. Java Byte类的compareTo()方法和示例

    简短的类compareTo()方法 (Short class compareTo() method) compareTo() method is available in java.lang pack ...

  5. Java短类的compareTo()方法和示例

    简短的类compareTo()方法 (Short class compareTo() method) compareTo() method is available in java.lang pack ...

  6. Java整数类的compareTo()方法和示例

    整数类compareTo()方法 (Integer class compareTo() method) compareTo() method is available in java.lang pac ...

  7. Java Long类的valueOf()方法及示例

    Syntax: 句法: public static Long valueOf (long value); public static Long valueOf (String value); publ ...

  8. Java Byte类的hashCode()方法及示例

    短类hashCode()方法 (Short class hashCode() method) hashCode() method is available in java.lang package. ...

  9. Java Thread类最终同步的void join(long time_in_ms)方法,带有示例

    线程类最终同步无效连接(long time_in_ms) (Thread Class final synchronized void join(long time_in_ms)) This metho ...

最新文章

  1. nagios全攻略(一)----准备阶段
  2. c语言程序设计字节,C语言程序设计-数据类型
  3. 让我们开一家医生假条商店吧/E
  4. jQuery Todolist
  5. 输入框输入数字,且不能有小数点存在
  6. Python基础学习:svn导出差异文件脚本
  7. xml、网络编程、 反射
  8. 【转】CentOS系统操作下安装相关各种软件
  9. jq+layui之保存修改的表状态
  10. java共享汽车租赁系统
  11. WinZip 18.0下载地址及注册码
  12. 机器学习数据的预处理
  13. 等额本金和等额本息是怎么算出来的
  14. 服务器屏幕不全屏显示,服务器窗口显示不全屏
  15. Java 异常(Exceptions)
  16. 场景应用:Redis使用setnx命令实现分布式锁
  17. Python攻防-截取屏幕并自动发送指定邮箱
  18. 2018 天梯赛及蓝桥杯比赛感想
  19. 使用Process Explorer和Dependency Walker排查C++程序中dll库动态加载失败问题
  20. 记录——python编程从入门到实践十二章练习题

热门文章

  1. Zookeeper分布式安装部署
  2. Mysql截取中英数混合的字符串
  3. 2019年春季学期第九周作业
  4. MySQL5.6 新特性之GTID【转】
  5. 给孩子一束安全的光 明基WiT MindDuo亲子共读灯首发评测
  6. 女人,向《奋斗》中的夏琳米莱们学习什么
  7. 古墓:黑夜中不断的断龙石敲击声
  8. css直接子元素怎么用,CSS 子元素选择器使用实例
  9. java占位符填充_Java使用freemark生成word
  10. 产品设计:APP个人信息保护指引