一、join()方法,官方描述 waits for this thread to die 等待当前线程死亡;
 
源码:
//无参,默认调用join(0)
public final void join() throws InterruptedException {
join(0);
}
 
//传入两时间millis 毫秒+nanos纳秒,表示等等millis+nanos,最终还是调用了方法3
public final synchronized void join(long millis, int nanos)
throws InterruptedException {
 
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
 
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
 
if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
millis++;
}
 
join(millis);
}
 
//方法3:传入等时间,单位为毫秒,底层调用Object的wait(time)
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
 
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
 
if (millis == 0) {
while (isAlive()) {
wait(0);//表示一直等待,指导线程死亡
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
 
 
二、应用,比如几个线程要按一定顺序执行
 
public class ThreadJoin extends Thread {
 
 
public void run(){
try {
this.sleep(500);
System.out.println("["+new Date()+"]"+this.getName());
 
} catch (InterruptedException e) {
e.printStackTrace();
}
}
 
 
public static void main(String[] args) throws Exception {
int length = 5;
Thread[] threads = new Thread[length];
for(int i=0; i<length;i++){
threads[i] = new ThreadJoin();
threads[i].start();
threads[i].join();//每个线程开启后都调用join
}
long endTime=System.currentTimeMillis();
}
}
/*
output:
[Sun Jun 11 13:40:42 CST 2017]Thread-0
[Sun Jun 11 13:40:43 CST 2017]Thread-1
[Sun Jun 11 13:40:43 CST 2017]Thread-2
[Sun Jun 11 13:40:44 CST 2017]Thread-3
[Sun Jun 11 13:40:44 CST 2017]Thread-4
 
 
 
如果把join()注释
结果可能每次都不一样
[Sun Jun 11 13:51:09 CST 2017]Thread-2
[Sun Jun 11 13:51:09 CST 2017]Thread-4
[Sun Jun 11 13:51:09 CST 2017]Thread-1
[Sun Jun 11 13:51:09 CST 2017]Thread-3
[Sun Jun 11 13:51:09 CST 2017]Thread-0
*/
 
 
 
 
做事不能急,给自己一个大方向后总得一步一步来
                  
 
 
 
 
 
 
 
 
 
 
 
 
 
 

线程-join();的更多相关文章

  1. 线程join理解

    1.python默认参数创建线程后,不管主线程是否执行完毕,都会等待子线程执行完毕才一起退出,有无join结果一样 2.如果创建线程,并且设置了daemon为true,即thread.setDaemo ...

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

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

  3. 【转】线程join()方法的含义

    在很多情况下,主线程生成并启动了子线程,如果子线程里要进行大量的耗时运算,主线程往往将于子线程之前结束,但是如果主线程处理完其它事务后,需要用到子线程的处理结果,也就是主线程需要等待子线程执行完成之后 ...

  4. python网络编程--线程join和Daemon(守护进程)

    一:什么情况下使用join join([timeout])调用join函数会使得主调线程阻塞,直到被调用线程运行结束或超时. 参数timeout是一个数值类型,用来表示超时时间,如果未提供该参数,那么 ...

  5. python线程join

    几个事实 1 python 默认参数创建线程后,不管主线程是否执行完毕,都会等待子线程执行完毕才一起退出,有无join结果一样 2 如果创建线程,并且设置了daemon为true,即thread.se ...

  6. java线程join方法使用方法简介

    本博客简介介绍一下java线程的join方法,join方法是实现线程同步,可以将原本并行执行的多线程方法变成串行执行的 如图所示代码,是并行执行的 public class ThreadTest { ...

  7. Python并发编程04 /多线程、生产消费者模型、线程进程对比、线程的方法、线程join、守护线程、线程互斥锁

    Python并发编程04 /多线程.生产消费者模型.线程进程对比.线程的方法.线程join.守护线程.线程互斥锁 目录 Python并发编程04 /多线程.生产消费者模型.线程进程对比.线程的方法.线 ...

  8. java线程join的意思(转自http://zjj1211.blog_51cto_com)

    Join,单词本事就是连接的意思. 先贴出几段代码猜猜结果. <1> public static int Main() { Alpha oAlpha = new Alpha(); Thre ...

  9. java-多线程-join函数

    join()>>不带参数 线程A调用线程B.join,意思就是线程A并入了线程B,当执行完线程B,再去执行线程A后续动作 join(int keepTims)>>带参数,与上面 ...

  10. 线程join

      class ThreadB extends Thread {     private String ID="0";     private int time=0;     pu ...

随机推荐

  1. openMP编程(下篇)之数据私有与任务调度

    title: openMP编程(下篇)之数据处理子句与任务调度 tags: ["openMP"] notebook: 分布式程序_Linux --- openMP并行编程中数据的共 ...

  2. HashMap 构造函数

    HashMap总共提供了三个构造函数 /** * Constructs an empty <tt>HashMap</tt> with the default initial c ...

  3. 记一次使用修改字节码的方法解决java.lang.NoSuchMethodError

    接兔兔国际sdk ane 充值界面选择兔币充值就会闪退, 观察logcat 04-19 10:10:54.224: E/AndroidRuntime(20315): FATAL EXCEPTION: ...

  4. 自定义TextView跑马灯

    本篇主要介绍TextView的可控制跑马灯效果实现. Android自带的TextView添加几个属性就可以实现跑马灯效果,大概是这样 android:ellipsize="marquee& ...

  5. Spring+SpringMVC+MyBatis+easyUI整合优化篇(十二)数据层优化-explain关键字及慢sql优化

    本文提要 从编码角度来优化数据层的话,我首先会去查一下项目中运行的sql语句,定位到瓶颈是否出现在这里,首先去优化sql语句,而慢sql就是其中的主要优化对象,对于慢sql,顾名思义就是花费较多执行时 ...

  6. 线段树区间更新操作及Lazy思想(详解)

    此题题意很好懂:  给你N个数,Q个操作,操作有两种,‘Q a b ’是询问a~b这段数的和,‘C a b c’是把a~b这段数都加上c. 需要用到线段树的,update:成段增减,query:区间求 ...

  7. 微坑---微信小程序ios上时间字符串转换为时间戳时,在开发工具上和安卓手机上运行成功

    给定一个时间字符串  var time="2017-02-27 16:42:53" js有三种转换为时间戳的方法:1.var timestamp = Date.parse(time ...

  8. Codeforces Round #410 (Div. 2)D题

    D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  9. poj1410计算几何线段相交

    You are to write a program that has to decide whether a given line segment intersects a given rectan ...

  10. VMware安装CentOS 6.7系统

    VMware安装CentOS 6.7系统 1. 安装前的准备 a) VMware虚拟机软件 b) CentOS 6.7镜像 c) Windows电脑一台 2. 开始安装 a) 打开VMware软件 b ...