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

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

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

  • join(long time_in_ms) method is applicable when currently executing thread wants to wait for a particular amount of time in milliseconds until completing some other thread then we should go for join(long time_in_ms) method of Thread class.

    join(long time_in_ms)方法适用于当前正在执行的线程想要以毫秒为单位的特定时间,直到完成其他线程为止,然后我们应该使用Thread类的join(long time_in_ms)方法。

  • This method is synchronized that is only one thread is allowed to operate one object.

    该方法是同步的,仅允许一个线程操作一个对象。

  • 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 two threads [t1 – PreparedExamPaper], [t2 – PrintingExamPaper] so will see what will happen.

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

Let suppose if a thread t1 executes, t2.join(1000), then thread t1 will entered into waiting state for 1000 milliseconds until t2 completes and suppose in case if t2 couldn't complete its execution in 1000 ms so in that case, t1 will get a chance to execute and if thread t1 goes into waiting state or sleep mode then again t2 will get a chance to execute its execution for 1000 ms and the same process will repeat.

假设如果线程t1执行了t2.join(1000) ,则线程t1将进入等待状态1000毫秒,直到t2完成为止,并假设t2无法在1000 ms内完成执行,因此在这种情况下, t1线程t1将有机会执行,并且如果线程t1进入等待状态或睡眠模式,则t2再次有机会执行其执行1000 ms,并且将重复相同的过程。

Syntax:

句法:

    final synchronized void join(long time_in_ms){
}

Parameter(s):

参数:

When we write t2.join(2000), so this line means currently executing thread will stop its execution for 2000 milliseconds until t2 completion.

当我们编写t2.join(2000)时 ,此行表示当前正在执行的线程将在2000毫秒内停止执行,直到t2完成。

Return value:

返回值:

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

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

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

/*  We will use Thread class methods so we are importing
the package but it is not mandate 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("Thread started:" + Thread.currentThread().getName());
try {Thread.sleep(500);
} catch (Exception ex) {System.out.println(ex.getMessage());
}
}
System.out.println("Thread Ended :" + Thread.currentThread().getName());
}
}
class MainThread1 {public static void main(String[] args) throws Exception {MyThread mt = new MyThread();
mt.start();
/* Note -1*/
mt.join(1000);
for (int j = 0; j < 2; ++j)
System.out.println("Thread started:" + Thread.currentThread().getName());
System.out.println("Thread ended:" + Thread.currentThread().getName());
}
}

Note1 : Here, we have written /*mt.join(1000)*/ means currently executing thread [main] will give a chance to another thread named [MyThread mt] for 1000 ms and then after main thread will get a chance to execute and if main thread goes into waiting for state or sleep mode then again MyThread will get a chance for 1000 ms and this repeats until complete execution of MyThread.

注意1:这里,我们已经编写了/*mt.join(1000)*/,表示当前正在执行的线程[main]将给另一个名为[ MyThread mt ]的线程一个机会,持续1000 ms,然后在主线程执行之后,有机会执行如果主线程进入等待状态或睡眠模式,则MyThread再次有机会获得1000毫秒的机会,并重复执行直到MyThread完全执行为止。

Output

输出量

E:\Programs>javac MainThread1.java
E:\Programs>java MainThread1
Thread started:Thread-0
Thread started:Thread-0
Thread started:main
Thread started:main
Thread ended:main
Thread started:Thread-0
Thread started:Thread-0
Thread started:Thread-0
Thread Ended :Thread-0

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

Java Thread类最终同步的void join(long time_in_ms)方法,带有示例相关推荐

  1. Java Thread类的最终void join()方法与示例

    线程类最终void join() (Thread Class final void join()) This method is available in package java.lang.Thre ...

  2. Java Thread类的静态布尔型interrupted()方法(带示例)

    线程类静态布尔型interrupted() (Thread Class static boolean interrupted()) This method is available in packag ...

  3. 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 ...

  4. Java Thread类的使用

    From:http://www.cnblogs.com/dolphin0520/p/3920357.html 一.线程的状态 在正式学习Thread类中的具体方法之前,我们先来了解一下线程有哪些状态, ...

  5. Java Thread类源码详解

    概述 Java所有多线程的实现,均通过封装Thread类实现,所以深入Thread类,对深入理解java多线程很有必要 构造函数: Thread的构造函数,采用缺省的方式实现: //传入Runnabl ...

  6. java thread类_java多线程之Thread类

    Class Thread java.lang.Object java.lang.Thread 实现接口:Runnable 直接被继承的子类:ForkJoinWorkerThread public cl ...

  7. java基础知识回顾之java Thread类学习(七)--java多线程安全问题(死锁)

    死锁:是两个或者两个以上的线程被无限的阻塞,线程之间互相等待所需资源. 线程死锁产生的条件: 当两个线程相互调用Join()方法. 当两个线程使用嵌套的同步代码块的时候,一个线程占用了另一个线程的锁, ...

  8. java thread类_Java多线程原理及Thread类详解

    多线程原理 代码如下: 自定义线程类: 测试类: 流程图: 程序启动运行main时候,java虚拟机启动一个进程,主线程main在main()调用时候被创建.随着调用mt的对象的start方法,另外一 ...

  9. java thread类是抽象类_Java继承抽象类Thread,实现接口Runnable,倒计时,线程休眠,静态变量【诗书画唱】...

    使用继承抽象类Thread方式创建一个线程,打印1到100之间的奇数设置其名称为线程1 package thread; public class ji { public static void mai ...

最新文章

  1. 部署SQL AZURE的客户端管理工具,云计算体验之二
  2. java异常_Java线程池「异常处理」正确姿势:有病就得治
  3. 循环控制_break语句
  4. DevNet网站上线
  5. ccBPM典型的树形表单和多表头表单的流程示例
  6. Leetcode--442. 数组中重复的数据
  7. linux位置变量的应用,llinux中变量的运用
  8. 2021年8月下旬好文收藏
  9. Spring Security 用户登录实战
  10. 电脑怎么在线录制屏幕声音,如何内录
  11. 用Altium Designer的databaseLib文件连接MySQL数据库工具管理自己的元器件信息数据库
  12. 高通 mdm9607编译以及audio框架
  13. word总页数不包含封面_word目录不包含封面 word 目录 不含封面
  14. Unity 基于图像处理的图像显示特效制作过场特效
  15. seo单页html模板,竞价单页模板设计思路
  16. I don't know her
  17. VS 点击页面自动定位到解决方案资源管理器目录位置
  18. 排序刷默认值sql脚本
  19. android 实现Sqlite的增删改查及系统的登录注册功能
  20. USB device for mac

热门文章

  1. TVM:使用 Auto-scheduling 来优化算子
  2. DVWA upload
  3. CTFHUB 《基础认证》:burp使用,basic请求了解
  4. java http 302重定向_Java 纯HTTP请求 禁止302自动重定向
  5. 局域网内文件传输速度_详解蒲公英路由器组网 实现文件共享
  6. 为什么python 为什么没有接口_python做接口测试的必要性
  7. Docker挂了,数据如何找回
  8. JDK源码解析之 Java.lang.StringBuffer
  9. JAVA知识基础(五):深入理解final关键字
  10. 自己动手实现一个html2canvas