thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程。比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B。或者说join() 的作用:让“主线程”等待“子线程”结束之后才能继续运行。
方法:
t.join(); //使调用线程 t 在此之前执行完毕。
t.join(1000); //等待 t 线程,等待时间是1000毫秒
public class TestThreadJoin { public static void main(String[] args){ System.out.println("system start time:"+System.currentTimeMillis()); Thread A = new Thread(){ @Override public void run() { try { sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } for(int i=0;i<10;i++){ System.out.println("current thread :"+ Thread.currentThread().getName());} System.out.println("current thread :"+ Thread.currentThread().getName()); System.out.println("A thread execute end time :"+ System.currentTimeMillis()); } }; Thread B = new Thread(A); System.out.println("A name:"+A.getName()); System.out.println("B name:"+B.getName()); A.start(); B.start(); try { System.out.println("current thread in main:"+Thread.currentThread().getName()); B.join(50); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("main end time:"+System.currentTimeMillis()); } }
system start time:1466060949347 A name:Thread-0 B name:Thread-1 current thread in main:main main end time:1466060949403 current thread :Thread-1 current thread :Thread-0 current thread :Thread-1 current thread :Thread-0 current thread :Thread-0 current thread :Thread-1 current thread :Thread-0 current thread :Thread-0 current thread :Thread-1 current thread :Thread-0 current thread :Thread-1 current thread :Thread-0 current thread :Thread-1 current thread :Thread-0 current thread :Thread-1 current thread :Thread-0 current thread :Thread-1 current thread :Thread-0 current thread :Thread-1 current thread :Thread-0 current thread :Thread-1 current thread :Thread-1 A thread execute end time :1466060949553 A thread execute end time :1466060949553
先上一段JDK中代码:
/** * 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() 方法是没有作用的,将直接继续向下执行
Join方法实现是通过wait(小提示:Object 提供的方法)。 当main线程调用t.join时候,main线程会获得线程对象t的锁(wait 意味着拿到该对象的锁),调用该对象的wait(等待时间),直到该对象唤醒main线程 ,比如退出后。这就意味着main 线程调用t.join时,必须能够拿到线程t对象的锁
class MyThread extends Thread {
public void run() {
System.out.println(getName() + "开始sleep" );
try {
Thread.sleep( 2000 );
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(getName() + "结束sleep" );
}
} public class TestWaitNotify {
public static void main(String[] args)
throws Exception {
Thread myThread = new MyThread();
myThread.start();
synchronized (myThread) {
myThread.wait();
}
System.out.println( "wait结束." );
}
} |
myThread执行结束后,main线程中的wait操作就自动退出了。程序里也并没有看到有notify/notifyAll调用。
如果将程序改成下面这样:
class MyThread extends Thread {
public void run() {
System.out.println(getName() + "开始sleep" );
try {
Thread.sleep( 2000 );
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(getName() + "结束sleep" );
}
} public class TestWaitNotify {
public static void main(String[] args)
throws Exception {
Thread myThread = new MyThread();
myThread.start();
Object tmp = new Object();
synchronized (tmp) {
tmp.wait();
}
System.out.println( "wait结束." );
}
} |
wait操作作用在一个独立的对象上,而不是像前面那样作用于线程对象上。这时程序就会一直wait下去。
给人直观的感觉,要么是发生了虚假唤醒,要么是线程在结束的时候做了些事情。经过查找,终于在JDK1.7 API文档的带参数的join方法上发现了蛛丝马迹!它是这么说的:
This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.
也就是说join的实现方式大概如下:
while ( this .isAlive()) {
this .wait();
} |
当线程结束的时候会调用notifyAll方法来让join方法返回,这个调用也影响了我们的应用程序。这也是为什么wait调用一定要放到一个循环中的因素之一。文档中也写明了不推荐我们的应用在Thread实例上调用wait, notify, or notifyAll。
在JDK1.7之前的API文档中是找不到这些描述的。碰到这样的问题只能去找JVM源码了。且在JDK1.7的文档中,这个信息还是出现在join方法上,调用wait/notify出问题的时候,如果事先不了解这个机制,又怎么会去看join方法的API呢!由此可见,一份好的JAVA DOC文档的重要性。
相关推荐
是第一次在C#中接触Thread,自己研究了一下其中Thread.Join()这个方法,下面谈谈自己的理解。 Thread.Join()在MSDN中的解释很模糊:Blocks the calling thread until a thread terminates 有两个主要问题: ...
CountDownLatch与thread.join()的区别
本文实例讲述了C#多线程之Thread中Thread.Join()函数用法。分享给大家供大家参考。具体分析如下: Thread.Join()在MSDN中的解释:Blocks the calling thread until a thread terminates 当NewThread调用Join方法的...
线程等待通常指的是一个线程(父线程)等待另一个线程(子线程)完成其工作,这通常通过`pthread_join()`函数实现,如`thread_join.c`所示。`pthread_join()`让调用线程阻塞,直到指定的线程结束。线程清除,或线程...
C++11中关于std::thread的join详解 C++11引入了std::thread库,提供了对多线程编程的支持。std::thread库提供了两个基本操作:join和detach。join操作用于回收线程资源,避免资源泄露;detach操作用于将线程与创建...
在Java编程语言中,`Thread.join()`方法是一个非常重要的同步工具,它允许一个线程(调用者)等待另一个线程(被调用者)执行完成。`Thread.join()`的使用能够有效地解决多线程环境中的顺序执行问题,确保线程间的...
Java 中 Thread.join() 的使用方法 Thread.join() 是 Java 中的一个重要方法,该方法允许一个线程等待另一个线程的执行完成。下面我们将详细介绍 Thread.join() 的使用方法、源码分析和示例代码。 1. join() 的...
JAVA CountDownLatch与thread-join()的区别解析 JAVA 中的线程同步机制是非常重要的,CountDownLatch 和 thread-join() 是两种常用的线程同步方法,本文将详细介绍它们之间的区别。 首先, thread-join() 方法是...
`join()`方法定义在`java.lang.Thread`类中,它使得多线程间的协作更加有序。 首先,我们来看`join()`的基本用法。在`Father`类(主线程类)中,创建了一个`Son`类(子线程类)的实例`s`,并调用`s.start()`启动子...
值得注意的是,join()方法可以指定等待的时间,即`thread.join(long millis)`,如果被join的线程在这段时间内没有结束,那么调用join()的线程将不再等待并继续执行。此外,join()还可以接受一个纳秒级别的参数,如`...
在Java多线程编程中,`Thread.join()`方法是一个非常重要的同步工具,它允许一个线程等待另一个线程结束执行后再继续执行。这个方法在处理并发控制和线程协作时非常有用,确保线程间的执行顺序。下面我们将深入探讨`...
1,简介 C++11中加入了<thread>头文件,此头文件主要声明了std::thread线程类。C++11的标准类std::thread对线程进行了封装,定义了C++11标准中的一些表示线程的...缺省构造的thread对象、已经完成join的thread对象、已
- **等待(Wait)**:线程因调用了`Sleep()`, `Join()`, `Monitor.Wait()`, 或其他阻塞方法而暂停执行。 - **挂起(Suspended)**:线程被挂起,无法执行,但内存占用仍然存在。 - **终止(Stopped/Terminated)*...
`join()`方法定义在`java.lang.Thread`类中,其主要功能是使调用该方法的线程(这里称为调用线程)暂停执行,直到目标线程(即`join()`方法所在的线程)执行完毕。这样,可以确保调用线程在目标线程执行完成后才...
thread::join $threadId puts "Main thread finished." ``` 这段代码会创建一个新的线程,并在其中打印一条消息,然后主线程等待新线程结束,最后主线程输出一条消息。需要注意的是,线程间的通信和同步可能需要...
Java线程Thread.join方法解析 Java线程中的join方法是一个非常重要的概念,它允许一个线程等待另一个线程的结束,以便进行后续的操作。在本篇文章中,我们将详细介绍Java线程Thread.join方法的解析和实现。 首先,...
my_thread.join(); // 等待my_thread执行完毕 my_thread.detach(); // my_thread将继续运行,主线程不再等待 ``` 在提供的压缩包文件"Boost_thread库示例"中,可能包含了以上各种用法的实例代码,你可以通过阅读...
《线程之道——深入解析Java中的Thread.join()》 在Java编程中,多线程是不可或缺的一部分,它能让我们在程序执行过程中同时处理多个任务,提高效率。而`Thread.join()`方法则是Java线程间协作的重要工具,它允许一...