扫描下方二维码或者微信搜索公众号
菜鸟飞呀飞
,便可关注微信公众号,阅读更多Spring源码分析
和Java并发编程
文章。java
public class JoinDemo {
public static void main(String[] args) {
System.out.println("肚子饿了,想吃饭饭");
Thread thread = new Thread(() -> {
System.out.println("开始作饭饭");
try {
// 让线程休眠10秒钟,模拟作饭的过程
Thread.sleep(10000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("饭饭作好了");
});
thread.start();
try {
// 等待饭作好
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
// 只有饭作好了,才能开始吃饭
System.out.println("开始吃饭饭");
}
}
复制代码
线程之间的通讯
。CyclicBarrier和CountDownLatch这两个类的做用的本质也是线程之间的通讯,在源码分析中,咱们能够发现,这两个类的底层最终是经过AQS来实现的,而AQS中是经过LockSupport类的park()
和unpark()
方法来实现线程通讯的。而Thread类的join()则是经过Object类的wait
和notify()、notifyAll()
方法来实现的。join(0)
方法,参数传0表示不限时长地等待
。join(long millis)方法的源码以下。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");
}
// 当millis为0时,表示不限时长地等待
if (millis == 0) {
// 经过while()死循环,只要线程还活着,那么就等待
while (isAlive()) {
wait(0);
}
} else {
// 当millis不为0时,就须要进行超时时间的计算,而后让线程等待指定的时间
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
复制代码
synchronized
关键字来保证线程安全,join(long millis)最终调用的是Object对象的wait()方法,让主线程等待。这里须要注意的是,synchronized关键字实现的隐式锁,锁的是this
,即thread这个对象(由于synchronized修饰的join(long millis)方法是一个成员方法,所以锁的是实例对象),在Demo中,咱们是在main线程中,调用了thread这个对象的join()方法,因此这里调用wait()方法时,调用的是thread这个对象的wait()方法,因此是main线程进入到等待状态中。(调用的是thread这个对象的wait()方法,这一点很重要,由于后面唤醒main线程时,须要用thread这个对象的notify()或者notifyAll()方法
)。确定是成对出现的
,单独使用它们毫无心义,那么在join()方法的使用场景下,notify()或者notifyAll()方法是在哪儿被调用的呢?答案就是jvm
。jdk-jdk8-b120/hotspot/src/share/vm/runtime/thread.cpp
(笔者本地下载的是openJDK8的源代码)。在thread.cpp文件中定义了不少和线程相关的方法,Thread.java类中的native方法就是在thread.cpp中实现的。JavaThread::exit(bool destroy_vm, ExitType exit_type)
方法(该方法的代码在1730行附近)。exit()方法的源码很长,差很少200行,删除了无用的代码,只保留了和今天主题有关的内容。源码以下。void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
assert(this == JavaThread::current(), "thread consistency check");
// ...省略了不少代码
// Notify waiters on thread object. This has to be done after exit() is called
// on the thread (if the thread is the last thread in a daemon ThreadGroup the
// group should have the destroyed bit set before waiters are notified).
// 关键代码就在这一行,从方法名就能够推断出,它是线程在退出时,用来确保join()方法的相关逻辑的。而这里的this就是指的当前线程。
// 从上面的英文注释也能看出,它是用来处理join()相关的逻辑的
ensure_join(this);
// ...省略了不少代码
}
复制代码
ensure_join()
方法中,接着找到ensure_join()
方法的源码,源码以下。(ensure_join()
方法的源码也在thread.cpp文件当中。有兴趣的朋友可使用JetBrains公司提供的Clion这款智能工具来查看JVM的源代码,和IDEA产很少,按住option(或者Alt键)+鼠标左键,也能跟踪到源代码中)。static void ensure_join(JavaThread* thread) {
// We do not need to grap the Threads_lock, since we are operating on ourself.
Handle threadObj(thread, thread->threadObj());
assert(threadObj.not_null(), "java thread object must exist");
ObjectLocker lock(threadObj, thread);
// Ignore pending exception (ThreadDeath), since we are exiting anyway
thread->clear_pending_exception();
// Thread is exiting. So set thread_status field in java.lang.Thread class to TERMINATED.
java_lang_Thread::set_thread_status(threadObj(), java_lang_Thread::TERMINATED);
// Clear the native thread instance - this makes isAlive return false and allows the join()
// to complete once we've done the notify_all below
java_lang_Thread::set_thread(threadObj(), NULL);
// 核心代码在下面这一行
// 是否是很惊喜?果真见到了和notify相关的单词,不用怀疑,notify_all()方法确定就是用来唤醒。这里的thread对象就是咱们demo中的子线程thread这个实例对象
lock.notify_all(thread);
// Ignore pending exception (ThreadDeath), since we are exiting anyway
thread->clear_pending_exception();
}
复制代码
CountDownLatch
或者CyclicBarrier
,由于后二者的功能更增强大。// 这种写法是等待/通知的经典范式。
while(条件判断){
wait();
}
复制代码