相信大家看api文档是不会搞清楚join()这个方法真正意义的,下面我用几行代码尽量言简意赅的说一下:
public class ThreadTest {
public static void main(String[] args) throws Exception {
ThreadTest tt = new ThreadTest();
tt.testJoin();
}
public void testJoin() throws Exception {
MyThread mt1 = new MyThread();
mt1.start();
long start = System.currentTimeMillis();
// mt1.join();
System.out.printf("testJoin over. Cost %d ms.\n", System.currentTimeMillis() - start);
}
}
class MyThread extends Thread {
public void run() {
System.out.println(this.getName() + " start...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName() + " over.");
}
}
运行上面的代码,得到的结果可能如下(第一句和第二句位置不固定)
Thread-0 start...
testJoin over. Cost 0 ms. //是的,这句会先执行
Thread-0 over.
是的,
System.out.printf("testJoin over. Cost %d ms.\n", System.currentTimeMillis() - start);
这一句会先于
System.out.println(this.getName() + " over.");
执行,且,打印出的 Cost 会是0ms。
这是因为 执行 mt1.start(); 后,会启一个Thread-0线程,主线程继续执行,而Thread-0线程则sleep 1000ms,
所以,就会出现上面的状况。
那么,我们将上面程序中 注释掉的那句mt1.join(); 加上,就会出现下面的结果:
Thread-0 start...
Thread-0 over.
testJoin over. Cost 1000 ms.
再看API文档中对join()方法的描述:等待该线程终止。 该线程是哪个线程?在上面的代码中,指的就是mt1.start();所创造出来的线程。
----------延伸-----------
而,如果我们给join 加上 参数,
第一次是:
mt1.join(30);
结果是:
Thread-0 start...
testJoin over. Cost 31 ms.
Thread-0 over.
第二次是:
mt1.join(2000);
结果是:
Thread-0 start...
Thread-0 over.
testJoin over. Cost 1000 ms.// 这里是1000,而不是2000
分析一下原因:
两种状况可导致join(long millis)方法退出: 主线程isAlive==false;等待毫秒数>millis。
PS:语言表达能力有限,可能说的不清楚。。。
分享到:
相关推荐
是第一次在C#中接触Thread,自己研究了一下其中Thread.Join()这个方法,下面谈谈自己的理解。 Thread.Join()在MSDN中的解释很模糊:Blocks the calling thread until a thread terminates 有两个主要问题: ...
主要介绍了c++11中关于std::thread的join详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
解决webrtc Thread::Clear崩溃的patch包,之前的老版本webrtc都会有Thread::Clear()多线程冲突的bug,根据这个补丁包,修改对应的源码,可以解决这个bug,亲测~~
CountDownLatch与thread.join()的区别
线程创建,线程等待与清除,thread_clean.c,thread_create.c,thread_create.c,thread_exit_struct.c,thread_id.c,thread_int.c, thread_join.c, thread_join.c, thread_string.c, thread_struct.c
(1)、get_id:获取线程ID,返回一个类型为std::thread::id的对象。 (2)、joinable:检查线程是否可被join。检查thread对象是否标识一个活动(active)的可行性线程。缺省构造的thread对象、已经完成join的thread对象、...
本文实例讲述了C#多线程之Thread中Thread.Join()函数用法。分享给大家供大家参考。具体分析如下: Thread.Join()在MSDN中的解释:Blocks the calling thread until a thread terminates 当NewThread调用Join方法的...
Thread基础知识点笔记总结
QObject调用moveToThread(QThread *targetThread)后, 运行targetThread.start(),此后QObject的事件将在targetThread中处理。 比如:QObject的槽函数,将在targetThread中处理。
JavaThread基础知识.pdf
五、 封装(面向对象特征之一)★★★★ 23 六、 继承(面向对象特征之一)★★★★ 25 七、 接口(面向对象特征之一)★★★★ 28 八、 多态(面向对象特征之一)★★★★ 30 九、 java.lang.Object 31 十、 异常★...
详细讲述了boost::thread的用法
JAVA语言的基础知识,主要讲解一些JAVA程序语言设计的基础知识,适合初学者。
这个是一份RT-Thread基础知识汇总,如果不想下载也可以参考博客https://blog.csdn.net/yuupengsun/category_9996721.html 也可以参考官网:https://www.rt-thread.org/ 共同学习。勿喷!勿喷!
Thread-Safe Collections 771 Callables and Futures 774 Executors 778 Synchronizers 785 Threads and Swing 794 Appendix 809 Index 813 Java 核心技术 卷2 Index Chapter 1: Streams and ...
Thread:我们可以开启一个线程。但是请大家记住:线程开启会在空间和时间上有不小的开销。所以,不能随便开。 ThreadPool:会根据你的CPU的核心数开启一个最合适的线程数量。如果你操作中,非常耗时,就不要用...
Keil RT-Thread Pack Installer File
c++11多线程库的使用,主要介绍了thread类中的构造函数 join函数和detach函数的使用方式
主要介绍了C++11 并发指南之std::thread 详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧