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", " ...
随机推荐
- oracle常用命令收集
1.查看监听状态 lsnrctl status 2.启动监听 lsnrctl start 3.关闭监听 lsnrctl stop 4.以数据库管理员登录 sqlplus / as sysdba 5.手 ...
- spring cloud + .net core实现微服务架构
1.新建spring boot项目 2.添加spring-cloud-starter-eureka-server依赖(需提供版本信息) <!-- https://mvnrepository.co ...
- Latex文件分别用Texwork和Winedt打开时,产生中文乱码的解决方法
中文兼容方法(能保证编译成功) \usepackage{CJK} \begin{document} \begin{CJK}{GBK}{kai} ... 中文 ... \end{CJK} \end{do ...
- ES 02 - 部署Elasticsearch单机服务 + 部署中的常见问题
目录 1 准备工作 1.1 安装JDK 1.2 下载安装包 1.3 创建elastic用户 2 启动ES服务 2.1 修改配置文件 2.2 启动服务 3 验证ES服务是否可用 4 关闭与重启服务 4. ...
- 你的第一个Django程序
本文使用Pycharm.Django 2.0.9.Python 3.6环境,本文大纲 建立Django项目 建立页面 什么是URLconf和ROOT_URLCONF Django怎么处理URL请求 关 ...
- Chapter 4 Invitations——11
"Bella?" His voice shouldn't have been so familiar to me, as if I'd known the sound of it ...
- Kali 无法正常上网问题
有时候我们会突然发现我们的kali不能够正常上网,在终端使用ping 命令对其进行检查,显示网络不可达, 然后使用ifconfig,可以看到没有正在工作的网卡,只有localhost 接着使用ifco ...
- gulp基本设置
var gulp = require('gulp'); var clean = require('gulp-clean'); var concat = require('gulp-concat'); ...
- python中循环删除列表中元素时的坑!
循环删除列表中元素时千万别用正序遍历,一定要用反序遍历! 废话不多说,先上案例代码: def test(data): for i in data: data.remove(i) return data ...
- 利用BGP虚拟下一跳实现链路负载均衡
最近针对BGP链路负载均衡方案“虚拟下一跳”进行了总结,现将总结的PPT贴上来.