join方法的作用是使所属线程对象正常执行run方法,而对当前线程无限期阻塞,直到所属线程销毁后再执行当前线程的逻辑。

一、先看普通的无join方法
NoJoin.java
public class NoJoin  extends  Thread{
@Override
public void run() { try {
long count = (long)(Math.random()*100);
System.out.println(count);
Thread.sleep(count);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class NoJoinTest {

    public static  void main(String[] args){

        NoJoin noJoin = new NoJoin();
noJoin.start();
System.out.println("执行到main....");
}
}

输出:

现在要先输出时间,再执行main方法。

只需要添加join方法即可。

/**
* Created by Administrator on 2016/1/5 0005.
*
* join方法的作用是使所属线程对象正常执行run方法,而对当前线程无限期阻塞,直到所属线程销毁后再执行当前线程的逻辑。
*/
public class JoinTest { public static void main(String[] args){ try {
NoJoin noJoin = new NoJoin();
noJoin.start();
noJoin.join();//join
System.out.println("执行到main....");
} catch (InterruptedException e) {
e.printStackTrace();
} }
}

查看join源代码:

 /**
* Waits at most {@code millis} milliseconds for this thread to
* die. A timeout of {@code 0} means to wait forever.
*
* <p> This implementation uses a loop of {@code this.wait} calls
* conditioned on {@code this.isAlive}. As a thread terminates the
* {@code this.notifyAll} method is invoked. It is recommended that
* applications not use {@code wait}, {@code notify}, or
* {@code notifyAll} on {@code Thread} instances.
*
* @param millis
* the time to wait in milliseconds
*
* @throws IllegalArgumentException
* if the value of {@code millis} is negative
*
* @throws InterruptedException
* if any thread has interrupted the current thread. The
* <i>interrupted status</i> of the current thread is
* cleared when this exception is thrown.
*/
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;
}
}
}

可见,join内部是对象的wait方法,当执行wait(mils)方法时,一定时间会自动释放对象锁。

[java] java 线程join方法详解的更多相关文章

  1. Java多线程中join方法详解

    join()方法用于让当前执行线程等待join线程执行结束.其实现原理是不停的检查join线程是否存活,如果join线程存活则让当前线程永远等待. join()方法部分实现细节 while(isAli ...

  2. 线程join方法详解

    执行逻辑:在当前代码块(比如main方法)中的线程A执行了join方法, 那么当代码块(main)执行到join方法时,会停止继续向下执行,一直到线程A执行完毕, main方法才会继续向下执行. 代码 ...

  3. “全栈2019”Java多线程第七章:等待线程死亡join()方法详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  4. “全栈2019”Java多线程第六章:中断线程interrupt()方法详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  5. “全栈2019”Java多线程第十二章:后台线程setDaemon()方法详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  6. Java中的main()方法详解

    在Java中,main()方法是Java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这个方法和其他的方法有很大的不同,比如方法的名字必须是main,方法必须是 ...

  7. 使用Java操作文本文件的方法详解

    使用Java操作文本文件的方法详解 摘要: 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而 ...

  8. Java并发编程--多线程中的join方法详解

    Java Thread中, join()方法主要是让调用该方法的thread在完成run方法里面的部分后, 再执行join()方法后面的代码 例如:定义一个People类,run方法是输出姓名年龄. ...

  9. java线程基础方法详解

    一.线程状态转换 1.新建状态(New):新创建了一个线程对象. 2.就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中,变得可运行 ...

随机推荐

  1. UBuntu14.04下安装和卸载Qt5.3.1

    安装: 1. Qt5.3.1下载地址为:http://qt-project.org/,选择”Qt 5.3.1 for Linux 32-bit”版本,文件名是”qt-opensource-linux- ...

  2. javascript 中XMLHttpRequest 实现前台向后台的交互

    使用XMLHttpRequest对象分为4部完成: 1.创建XMLHttpRequest组建 2.设置回调函数 3.初始化XMLHttpRequest组建 4.发送请求

  3. Maven项目下HttpServletRequest 或 HttpServletResponse需引用的依赖包

    转载: http://xyly624.blog.51cto.com/842520/865630/ Maven项目下HttpServletRequest 或 HttpServletResponse需引用 ...

  4. sql插入数据

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. 基于js仿汽车之家2015新版焦点图代码

    今天给大家分享一款仿汽车之家2015新版焦点图代码.这是一款基于jQuery实现的适合电子商务网站或者企业产品展示功能特效.效果图如下: 在线预览   源码下载 实现的代码. html代码: < ...

  6. tcp与http的区别

    1.TCP连接 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接.TCP协议可以对上层网络提供接口,使上层网络数据的传输建立在“无差别”的网络之上. ...

  7. CKFinder 自定义文件路径扩展ConfigurationPathBuilder

    CKFinder 自定义文件路径扩展ConfigurationPathBuilder 打开config.xml当中可以看到如下配置 <basePathBuilderImpl>com.ckf ...

  8. NFS服务的端口分配

    常规的一些NFS服务设置我们已经了解了.那么对于端口问题,很多朋友并不是很清楚.这里我们就来详细介绍一下端口的分配.portmapper在NFS服务启动的时候给每一个NFS服务分配了一个动态的端口,如 ...

  9. 实现字符串转化为整数函数atoi()函数

    函数原型: int atoi(const char *nptr); 函数说明: 参数nptr字符串,如果第一个非空格字符存在,并且,如果不是数字也不是正负号则返回零,否则开始做类型转换,之后检测到非数 ...

  10. 用大白话揭开Ajax长轮询(long polling)的神秘面纱

    在看这篇Ajax长轮询之前可以先看看Ajax轮询技术(没有长),有助于理解: Ajax长轮询属于Ajax轮询的升级版,在客户端和服务端都进行了一些改造,使得消耗更低,速度更快. "不间断的通 ...