package com.wangwenjun.concurrency.chapter5;

public class ThreadJoin3 {

    public static void main(String[] args) throws InterruptedException {
long startTimestamp = System.currentTimeMillis();
Thread t1 = new Thread(new CaptureRunnable("M1", 10000L));
Thread t2 = new Thread(new CaptureRunnable("M2", 30000L));
Thread t3 = new Thread(new CaptureRunnable("M3", 15000L)); t1.start();
t2.start();
t3.start(); t1.join();
t2.join();
t3.join();
//join可以将上面三个线程并行执行,但不执行下面的线程,等待上面线程执行后再执行下面的输出
long endTimestamp = System.currentTimeMillis();
System.out.printf("Save data begin timestamp is:%s, end timestamp is:%s\n", startTimestamp, endTimestamp);
} } 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 successfully.\n", System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
} public String getResult() {
return machineName + " finish.";
}
}

多线程07.thread-join的更多相关文章

  1. 多线程进阶---Thread.join()/CountDownLatch.await() /CyclicBarrier.await()

    Thread.join() CountDownLatch.await() CyclicBarrier.await() 三者都是用来控制程序的"流动" 可以让程序"堵塞&q ...

  2. java多线程同步以及线程间通信详解&消费者生产者模式&死锁&Thread.join()(多线程编程之二)

    本篇我们将讨论以下知识点: 1.线程同步问题的产生 什么是线程同步问题,我们先来看一段卖票系统的代码,然后再分析这个问题: package com.zejian.test; /** * @author ...

  3. 多线程:Thread类的Join()方法

    多线程:Thread类的Join()方法 http://blog.163.com/hc_ranxu/blog/static/3672318220095284513678/ 当我们在线程B中调用Thre ...

  4. C#多线程详解(一) Thread.Join()的详解

    bicabo   C#多线程详解(一) Thread.Join()的详解 什么是进程?当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程 ...

  5. 多线程编程(一) - 关于C#中Thread.Join()

    Thread.Join()在MSDN中的解释很模糊:Blocks the calling thread until a thread terminates 有两个主要问题:1.什么是the calli ...

  6. 多线程:head first Thread.join()

    不使用Thread.join() 测试线程 先上代码: /** * Created by Zero on 2017/8/23. */ public class TestJoin implements ...

  7. C#多线程Thread.Join()的详解

    class TestThread { private static void FirstThreadFun() { ; i < ; i++) { Console.WriteLine(Thread ...

  8. 多线程--Thread.join方法

    在Thread类的Api中,Join的作用是让当前线程等待目标线程结束之后才继续执行. thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.  比如在线程B ...

  9. java 多线程 Thread.join子线程结束父线程再运行;join(long):等待超时毫秒数

    Join的使用 目的:当子线程运行结束后,父线程才能再继续运行 /** * @ClassName ThreadJoinExample * @projectName: object1 * @author ...

  10. 关于python多线程编程中join()和setDaemon()的一点儿探究

    关于python多线程编程中join()和setDaemon()的用法,这两天我看网上的资料看得头晕脑涨也没看懂,干脆就做一个实验来看看吧. 首先是编写实验的基础代码,创建一个名为MyThread的  ...

随机推荐

  1. mysql join语句的执行流程是怎么样的

    mysql join语句的执行流程是怎么样的 join语句是使用十分频繁的sql语句,同样结果的join语句,写法不同会有非常大的性能差距. select * from t1 straight_joi ...

  2. C 标准库函数手册摘要

    <stdlib.h> int abs( int value ); long int labs( long int value ); 返回参数的绝对值 int rand( void ); v ...

  3. 【转-Andrew_qian】stm32中断嵌套全攻略

    断断续续学习STM32一学期了,时间过的好快,现在对STM32F103系列单片机的中断嵌套及外部中断做一个总结,全当学习笔记.废话不多说,ARM公司的Cortex-m3 内核,支持256个中断,其中包 ...

  4. Git浅析

    Git浅析 索引 Git的常用命令 GitHub的使用 Git版本创建和回退 Git的工作区和暂存区 Git分支管理 1-Git的常用命令 01.创建一个版本库--进入相应的目录 git init 可 ...

  5. spring cloud zuul的回退

    当我们使用 @EnableZuulProxy 注解来开启zuul的路由时,默认在@EnableZuulProxy注解上就包含了@EnableCircuitBreaker注解,即开启了断路器功能.那么在 ...

  6. str数组

  7. 野指针和free总结超有用的资料

    在C语言项目中,经常会遇到需要程序员手动分配内存的地方.这样做能够节省大量的内存空间,也让程序更加灵活.只要你有一定的基础,那么肯定用过 malloc 或者 ralloc和free的组合.这个组合使用 ...

  8. Spring Cloud Alibaba 使用Nacos作为配置管理中心

    为什么需要配置中心? 动态配置管理是 Nacos 的三大功能之一,通过动态配置服务,我们可以在所有环境中以集中和动态的方式管理所有应用程序或服务的配置信息. 动态配置中心可以实现配置更新时无需重新部署 ...

  9. hdu 1503 Advanced Fruits(DP)

    题意: 将两个英文单词进行合并.[最长公共子串只要保留一份] 输出合并后的英文单词. 思路: 求最长公共子串. 记录路径: mark[i][j]=-1:从mark[i-1][j]转移而来. mark[ ...

  10. diff 命令,防止遗忘

    常规输出: diff 1.file 2.file 并排格式输出: diff 1.file 2.file -y -W 50 显示说明 "|"表示前后2个文件内容有不同 "& ...