Thread的join方法demo

/**
* 关于join官方的解释是 Waits for this thread to die. 也就是等待一个线程结束。
*/
public class ThreadJoinTest {
public static void main(String[] args) throws InterruptedException {
long startTimestamp = System.currentTimeMillis(); // 假设有三台机器,开启三个线程。
Thread m1 = new Thread(new CaptureRunnable("M1", 1_000L));
Thread m2 = new Thread(new CaptureRunnable("M2", 2_000L));
Thread m3 = new Thread(new CaptureRunnable("M3", 3_000L)); /**
* 可以看到三个线程还没走完,就提前把时间打印出来了,这个不是我我们想要的效果,那么我们让三个线程join一下试试:
* Save data begin timestamp is 1665222904024, end timestamp is 1665222904025
* Spend time is 1
* M1 completed data capture at timestamp [1665222905026] and successful.
* M2 completed data capture at timestamp [1665222906025] and successful.
* M3 completed data capture at timestamp [1665222907026] and successful.
*/
m1.start();
m2.start();
m3.start(); /**
*
* 打开注释输出:
* M1 completed data capture at timestamp [1665222874569] and successful.
* M2 completed data capture at timestamp [1665222875569] and successful.
* M3 completed data capture at timestamp [1665222876569] and successful.
* Save data begin timestamp is 1665222873568, end timestamp is 1665222876569
* Spend time is 3001
*/
// m1.join();
// m2.join();
// m3.join(); long endTimestamp = System.currentTimeMillis(); System.out.printf("Save data begin timestamp is %s, end timestamp is %s\n", startTimestamp, endTimestamp);
System.out.printf("Spend time is %s\n", endTimestamp - startTimestamp);
} /**
* 采集服务器节点的任务。
*/
static class CaptureRunnable implements Runnable {
// 机器节点的名称
private String machineName;
// 采集花费时间
private long spendTime; public CaptureRunnable(String machineName, long spendTime) {
this.machineName = machineName;
this.spendTime = spendTime;
} @Override
public void run() {
// do the really capture data.
try {
Thread.sleep(spendTime);
System.out.printf(machineName + " completed data capture at timestamp [%s] and successful.\n", System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
} public String getResult() {
return machineName + " finish.";
}
} }

Thread的join方法demo的更多相关文章

  1. 浅析Thread的join() 方法

    Thread中的 join() 方法在实际开发过程中可能用的不是很多,但是在面试中作为考察基本功知识的扎实与否,经常会被用到.因此,对于 Thread 的 join() 方法进行了一定的研究. 常见的 ...

  2. Thread线程join方法自我理解

    Thread线程join方法自我理解 thread.join():等待thread线程运行终止,指的是main-thread(main线程)必须等待thread线程运行结束,才能继续thread.jo ...

  3. Thread 的join方法

    package com.cn.test.thread; public class TestJoin extends Thread{ private String name; public TestJo ...

  4. 8.Thread的join方法

    1.Thread类的join方法表示:当前线程执行结束再执行其它线程!在Thread类中有三个重载的方法分别是: public final synchronized void join(long mi ...

  5. python thread的join方法解释

    python的Thread类中提供了join()方法,使得一个线程可以等待另一个线程执行结束后再继续运行.这个方法还可以设定一个timeout参数,避免无休止的等待.因为两个线程顺序完成,看起来象一个 ...

  6. java线程基础巩固---分析Thread的join方法详细介绍,结合一个典型案例

    关于Thread中的join方法貌似在实际多线程编程当中没怎么用过,在当初学j2se的时候倒时去学习过它的用法,不过现在早已经忘得差不多啦,所以对它再复习复习下. 首先先观察下JDK对它的介绍: 其实 ...

  7. Thread的join方法

    一个线程在执行的过程中,可能调用另一个线程,前者可以称为调用线程,后者成为被调用线程. Thread.Join方法的使用场景:调用线程挂起,等待被调用线程执行完毕后,继续执行. 如下案列: 当NewT ...

  8. Thread中join()方法进行介绍

    http://www.cnblogs.com/skywang12345/p/3479275.html https://blog.csdn.net/dabing69221/article/details ...

  9. 浅谈Java多线程中的join方法

    先上代码 新建一个Thread,代码如下: package com.thread.test; public class MyThread extends Thread { private String ...

  10. Java多线程中的join方法

    新建一个Thread,代码如下: package com.thread.test; public class MyThread extends Thread { private String name ...

随机推荐

  1. 使用云效Codeup10分钟紧急修复Apache Log4j2漏洞

    ​简介:2021年12月10日,国家信息安全漏洞共享平台(CNVD)收录了Apache Log4j2远程代码执行漏洞(CNVD-2021-95914),此漏洞是一个基于Java的日志记录工具,为Log ...

  2. [Kali] Kali Linux 环境准备

      虚拟机和系统: Mac 的 Vmware Fusion:https://www.vmware.com/cn/products/fusion/fusion-evaluation.html  序列号去 ...

  3. [FAQ] 如何从 svg 字符串创建 SVGElement

      HTML 部分: <div style="display: none" id="svgCon"></div> JS 部分: cons ...

  4. [Contract] 一次搞懂 Solidity 的 using xx for xx

    using A for *;    # 把 A 的函数附给任意类型使用 using A for B; # 意思是把 A 中的方法附给 B 使用 使用上面的方式,那么在我们的合约中定义了 B 类型的变量 ...

  5. [PHP] 浅谈 Laravel 三大验证方式的区别, auth:api, passport, auth:airlock

    auth:api 最先出来,提供了最简单和最实用的方式进行 api 身份校验. 关于它的含义和用法你可以参考以下两篇: 浅谈 Laravel Authentication 的 auth:api 浅谈 ...

  6. VP NOI2023

    一个月前的事情捏,因为今天刚好在摸鱼就想起来写写. Day 1 开题,先总的过一遍,好像比较传统. T1 基本上是一眼题了,简单容斥一下就可以解决.很快开始写,写好过了小样例.但是这个时候还没有大样例 ...

  7. 安装pyenv-win(windows 环境)支持多个python环境管理

    安装pyenv-win(windows 环境)支持多个python环境管理 https://blog.csdn.net/dair6/article/details/129128240

  8. Git/SourceTree版本管理

    目录 视频课程: 工作区: 文件状态: 回退版本: 合并分支 合并提交 冲突 删除分支 忽略文件 汉英对照表 多端同步 添加远程仓库 推送代码到远程仓库 拉取代码 视频课程: https://www. ...

  9. 号外:Splashtop与Jira发布新的远程支持集成

    首先个跟大家道个歉,近期最近因为技术调整等原因,官网自动跳转中文站失效,可能很多宝宝看了一周的英文站了.程序哥们还在加班加点解决这个问题.如果大佬们也遇到了这个情况,官网后面加个/cn就可以了. 别问 ...

  10. Selenium4自动化测试6--控件获取数据--下拉框级联选择、checkbox选择、时间选择器

    4-下拉框级联选择 import time from selenium.webdriver.support.select import Select #pip install selenium fro ...