JAVA THREAD.JOIN方法详解
一、使用方式。
join是Thread类的一个方法,启动线程后直接调用,例如:
Thread t = new AThread(); t.start(); t.join();
二、为什么要用join()方法
在很多情况下,主线程生成并起动了子线程,如果子线程里要进行大量的耗时的运算,主线程往往将于子线程之前结束,但是如果主线程处理完其他的事务后,需要用到子线程的处理结果,也就是主线程需要等待子线程执行完成之后再结束,这个时候就要用到join()方法了。
三、join方法的作用
在JDk的API里对于join()方法是:
join
public final void join() throws InterruptedException Waits for this thread to die. Throws: InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
即join()的作用是:“等待该线程终止”,这里需要理解的就是该线程是指的主线程等待子线程的终止。也就是在主线程调用了join()方法后面的代码,只有等到子线程结束了才能执行。
四、用实例来理解
写一个简单的例子来看一下join()的用法:
class BThread extends Thread {
                public BThread() {
                    super("[BThread] Thread");
                };
                public void run() {
                    String threadName = Thread.currentThread().getName();
                    System.out.println(threadName + " start.");
                    try {
                        for (int i = 0; i < 5; i++) {
                            System.out.println(threadName + " loop at " + i);
                            Thread.sleep(1000);
                        }
                        System.out.println(threadName + " end.");
                    } catch (Exception e) {
                        System.out.println("Exception from " + threadName + ".run");
                    }
                }
 }
class AThread extends Thread {
               BThread bt;
                public AThread(BThread bt) {
                    super("[AThread] Thread");
                    this.bt = bt;
                }
                public void run() {
                    String threadName = Thread.currentThread().getName();
                    System.out.println(threadName + " start.");
                    try {
                        bt.join();
                        System.out.println(threadName + " end.");
                    } catch (Exception e) {
                        System.out.println("Exception from " + threadName + ".run");
                    }
                }
}
public class TestDemo {
                public static void main(String[] args) {
                    String threadName = Thread.currentThread().getName();
                    System.out.println(threadName + " start.");
                    BThread bt = new BThread();
                    AThread at = new AThread(bt);
                    try {
                        bt.start();
                        Thread.sleep(2000);
                        at.start();
                        at.join();
                    } catch (Exception e) {
                        System.out.println("Exception from main");
                    }
                    System.out.println(threadName + " end!");
                }
 }
打印结果:
main start. //主线程起动,因为调用了at.join(),要等到at结束了,此线程才能向下执行。
[BThread] Thread start.
[BThread] Thread loop at 0
[BThread] Thread loop at 1
[AThread] Thread start. //线程at启动,因为调用bt.join(),等到bt结束了才向下执行。
[BThread] Thread loop at 2
[BThread] Thread loop at 3
[BThread] Thread loop at 4
[BThread] Thread end.
[AThread] Thread end. // 线程AThread在bt.join();阻塞处起动,向下继续执行的结果
main end! //线程AThread结束,此线程在at.join();阻塞处起动,向下继续执行的结果。
修改一下代码:
 public class TestDemo {
                public static void main(String[] args) {
                    String threadName = Thread.currentThread().getName();
                    System.out.println(threadName + " start.");
                    BThread bt = new BThread();
                    AThread at = new AThread(bt);
                    try {
                        bt.start();
                        Thread.sleep(2000);
                        at.start();
                        //at.join(); //在此处注释掉对join()的调用
                    } catch (Exception e) {
                        System.out.println("Exception from main");
                    }
                    System.out.println(threadName + " end!");
                }
}
打印结果:
main start. // 主线程起动,因为Thread.sleep(2000),主线程没有马上结束;
[BThread] Thread start. //线程BThread起动
[BThread] Thread loop at 0
[BThread] Thread loop at 1
main end! // 在sleep两秒后主线程结束,AThread执行的bt.join();并不会影响到主线程。
[AThread] Thread start. //线程at起动,因为调用了bt.join(),等到bt结束了,此线程才向下执行。
[BThread] Thread loop at 2
[BThread] Thread loop at 3
[BThread] Thread loop at 4
[BThread] Thread end. //线程BThread结束了
[AThread] Thread end. // 线程AThread在bt.join();阻塞处起动,向下继续执行的结果
五、从源码看join()方法
在AThread的run方法里,执行了bt.join();,进入看一下它的JDK源码:
public final void join() throws InterruptedException {
                join(0L);
}
然后进入join(0L)方法:
public final synchronized void join(long l) throws InterruptedException{
    long l1 = System.currentTimeMillis();
    long l2 = 0L;
    if(l < 0L)
        throw new IllegalArgumentException("timeout value is negative");
    if(l == 0L)
        for(; isAlive(); wait(0L));
    else
        do
        {
            if(!isAlive())
                break;
            long l3 = l - l2;
            if(l3 <= 0L)
                break;
            wait(l3);
            l2 = System.currentTimeMillis() - l1;
        } while(true);
}
单纯从代码上看: * 如果线程被生成了,但还未被起动,isAlive()将返回false,调用它的join()方法是没有作用的。将直接继续向下执行。 * 在AThread类中的run方法中,bt.join()是判断bt的alive状态,如果bt的isAlive()方法返回false,在 bt.join(),这一点就不用阻塞了,可以继续向下进行了。从源码里看,wait方法中有参数,也就是不用唤醒谁,只是不再执行wait,向下继续执行而已。 * 在join()方法中,对于isAlive()和wait()方法的作用对象是个比较让人困惑的问题:
  isAlive()方法的签名是:public final native boolean isAlive(),也就是说isAlive()是判断当前线程的状态,也就是bt的状态。
      wait()方法在jdk文档中的解释如下:
      Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).
      The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
在这里,当前线程指的是at。
JAVA THREAD.JOIN方法详解的更多相关文章
- [java] java 线程join方法详解
		
join方法的作用是使所属线程对象正常执行run方法,而对当前线程无限期阻塞,直到所属线程销毁后再执行当前线程的逻辑. 一.先看普通的无join方法NoJoin.java public class N ...
 - “全栈2019”Java多线程第七章:等待线程死亡join()方法详解
		
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
 - C#多线程详解(一) Thread.Join()的详解
		
bicabo C#多线程详解(一) Thread.Join()的详解 什么是进程?当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程 ...
 - java的sleep方法详解
		
java的sleep方法详解: sleep并不是永久占有CPU,没有那个线程能永久占用CPU.它是指在自己时间片内睡眠,而不是急着交出CPU.yield()就是自己愿意立即交出时间片.因此一个线程sl ...
 - C#多线程Thread.Join()的详解
		
class TestThread { private static void FirstThreadFun() { ; i < ; i++) { Console.WriteLine(Thread ...
 - Thread.Join()的详解
		
什么是进程?当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程所组成的. 什么是线程?线程是程序中的一个执行流,每个线程都有自己的专有寄 ...
 - Java并发编程--多线程中的join方法详解
		
Java Thread中, join()方法主要是让调用该方法的thread在完成run方法里面的部分后, 再执行join()方法后面的代码 例如:定义一个People类,run方法是输出姓名年龄. ...
 - Java多线程中join方法详解
		
join()方法用于让当前执行线程等待join线程执行结束.其实现原理是不停的检查join线程是否存活,如果join线程存活则让当前线程永远等待. join()方法部分实现细节 while(isAli ...
 - Java多线程——多线程方法详解
		
本系列文章是Java多线程的详解介绍,对多线程还不熟悉的同学可以先去看一下我的这篇博客Java基础系列3:多线程超详细总结,这篇博客从宏观层面介绍了多线程的整体概况,接下来的几篇文章是对多线程的深入剖 ...
 
随机推荐
- ATS来了,网页HTTP访问怎么办?
			
推荐理由 ATS(App Transport Security),是苹果在WWDC 15提出的,苹果将收紧http的访问,这样会造成我们周边的许多站点和应用均不能正常访问,这里就对ATS进行了简单分析 ...
 - jquery列队动画简单演示
			
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
 - iOS开发寻找最近公共view
			
新技能 #pragma mark --寻找最近公共view + (NSArray *)superViews:(UIView *)view{ if (view==nil) { return @[]; } ...
 - html中引入调用另一个html的方法
			
html中引入调用另一个html的方法,尝试了好几种,都列出来: 其中第一种是最好的,其他的方法,可以尝试看看,是不是适合你当前项目 一.div+$("#page1").load( ...
 - Spring Hiernate整合
			
Spring整合Hibernate 一.整合目标 1.由IoC容器管理Hibernate的SessionFactory2.让Hibernate使用Spring的声明式事务 二.整合步骤 先加入Hibe ...
 - sublimeText3插件安装
			
1,官方下载sublimeText 3(百度搜索) 2,安装成功后按Ctrl+`调出console 3,然后输入 import urllib.request,os; pf = 'Package Con ...
 - 2017.3.12 H5学习的第一周
			
本周我开始了H5的学习,在这一周里我们从html的基本标签开始一直讲到了才算css的用法,接下来我将记录下来本周我学到的H5的内容. 首先是声明文档,声明文档类型是HTML5文件,它在HTML文档必不 ...
 - 1134: 零起点学算法41——多组测试数据(a+b)III
			
1134: 零起点学算法41--多组测试数据(a+b)III Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitt ...
 - jmeter JDBC 连接数据库
			
1.添加JDBC Connection Configuration 2.添加JDBC Request 3.添加查看结果树 4. 设置下列参数:Database URL:jdbc:mysql://hos ...
 - 如何选择适合自己的CMS建站系统
			
在互联网发展突飞猛进的今天,可谓”得市场者得天下”,而电商已成为占据市场速度最快.范围最广的手段,进而网站建设也逐渐引起商家重视,并深入到了企业和个体的发展战略之中. 如今企业建站已 ...