Thread中的join()方法
package com.qjc.thread;
public class JoinExcemple {
// join方法就是用来同步的
public static void main(String[] args) throws InterruptedException {
ThreadJoinTest threadJoinA = new ThreadJoinTest("线程A");
ThreadJoinTest threadJoinB = new ThreadJoinTest("线程B");
Thread threadA = new Thread(threadJoinA);
Thread threadB = new Thread(threadJoinB);
threadA.start();
threadA.join();// 在main线程中,调用threadA线程的join方法,主线程就会放弃cpu,等待threadA执行完毕后再执行
threadB.start();
}
}
class ThreadJoinTest implements Runnable {
String threadName;
public ThreadJoinTest(String threadName) {
this.threadName = threadName;
}
@Override
public void run() {
for (int i = 0; i < 100000; i++) {
System.out.println(this.threadName + "----" + i);
}
}
}
大家可以看一下join()方法的源码
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);//如果活着则等待,参数为0,说明必须等待调用join()方法的线程执行完毕后再执行
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
/**
* Tests if this thread is alive. A thread is alive if it has
* been started and has not yet died.
*
* @return <code>true</code> if this thread is alive;
* <code>false</code> otherwise.
* 如果该线程已经启动,但是还没有结束,则就是活着的
*/
public final native boolean isAlive(); /**
* Causes the current thread to wait until either another thread invokes the
* {@link java.lang.Object#notify()} method or the
* {@link java.lang.Object#notifyAll()} method for this object, or a
* specified amount of time has elapsed.
* 该线程会一直等待,直到被唤醒
*/
public final native void wait(long timeout) throws InterruptedException;
所以,从源码可以看出,主线程中调用了ThreadA的join()方法,则相当于调用了ThreadA的wait()方法,
当ThreadA线程执行完后会调用自身的notifyAll()方法唤醒主线程,从而达到了同步的目的
Thread中的join()方法的更多相关文章
- Java并发编程--多线程中的join方法详解
Java Thread中, join()方法主要是让调用该方法的thread在完成run方法里面的部分后, 再执行join()方法后面的代码 例如:定义一个People类,run方法是输出姓名年龄. ...
- Thread类中的join方法
package charpter06; //类实现接口public class Processor implements Runnable { // 重写接口方法 @Override public v ...
- Thread中,join()方法
Thread中,join()方法的作用是调用线程等待该线程完成后,才能继续用下运行. public static void main(String[] args) throws Interrupted ...
- Java多线程中的join()方法
一.join()方法介绍 join() 定义在Thread.java中.join()方法把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A的join( ...
- Thread类的join()方法
public class Demo { /** * Thread类的join()方法 * -------------------------------- * 1)join() * 2)join(lo ...
- Thread中的一些方法
Thread中的一些方法: currentThread() 返回对当前正在执行的线程对象的引用(实现接口方式时使用). sleep(long millis) 在指定的毫秒数内让当前正在执行的线程休眠( ...
- 【线程】Thread中的join介绍
因为sleep.wait.join等阻塞,可以使用interrupted exception异常唤醒. 一.作用 Thread类中的join方法的主要作用就是同步,它可以使得线程之间的并行执行变为串行 ...
- 多线程:Thread类的Join()方法
多线程:Thread类的Join()方法 http://blog.163.com/hc_ranxu/blog/static/3672318220095284513678/ 当我们在线程B中调用Thre ...
- jQuery中的join方法
和JS 中的JOIN 方法一样,将一数组按照JOIN的参数连接起来.比如: var arr = [ "a", "b", "c", " ...
随机推荐
- 编译安装mysql5.7.24踩的坑
1.报错如下:CMake Error at cmake/boost.cmake:76 (MESSAGE): You can download it with -DDOWNLOAD_BOOST=1 - ...
- Spring Boot Cache配置 序列化成JSON字符串
当我们使用@Cacheable注解的时候会将返回的对象缓存起来,我们会发现默认缓存的值是二进制的,不方便查看,为此我们自定义序列化配置,改成JSON格式的 配置如下: pom.xml <?xml ...
- asp.net core 系列 9 环境(Development、Staging 、Production)
一.在asp.net core中使用多个环境 ASP.NET Core 配置是基于运行时环境, 使用环境变量.ASP.NET Core 在应用启动时读取环境变量ASPNETCORE_ENVIRONME ...
- redis 系列27 Cluster高可用 (2)
一. ASK错误 集群上篇最后讲到,对于重新分片由redis-trib负责执行,关于该工具以后再介绍.在进行重新分片期间,源节点向目标节点迁移一个槽的过程中,可以会出现该槽中的一部分键值对保存在源节点 ...
- 《HelloGitHub月刊》第 11 期
<HelloGitHub>第 11 期 兴趣是最好的老师,<HelloGitHub>就是帮你找到兴趣! 简介 最开始我只是想把自己在浏览 GitHub 过程中,发现的有意思.高 ...
- springboot+springcloud集成jar
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 设计模式总结篇系列:建造者模式(Builder)
关于建造者模式网上有很多文章,也有些不同的理解.在此结合网上其他文章对建造者模式进行总结. 总体说来,建造者模式适合于一个具有较多的零件(属性)的产品(对象)的创建过程.根据产品创建过程中零件的构造是 ...
- 图数据库项目DGraph的前世今生
本文由云+社区发表 作者:ManishRai Jain 作者:ManishRai Jain Dgraph Labs创始人 版权声明:本文由腾讯云数据库产品团队整理,页面原始内容来自于db weekly ...
- Java Socket通信实现私聊、群聊
前言 闲言少叙,上代码! 代码编写 server服务端 /** * 服务端 */ public class Server { private static ServerSocket server = ...
- C# Task 篇幅一
在https://www.cnblogs.com/loverwangshan/p/10415937.html中我们有讲到委托的异步方法,Thread,ThreadPool,然后今天来讲一下Task, ...