浅谈Java多线程中的join方法
先上代码
新建一个Thread,代码如下:
package com.thread.test;
public class MyThread extends Thread {
private String name;
public MyThread(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(name+"["+i+"]");
}
super.run();
}
}
之后新建测试类,代码如下:
package com.thread.test;
/*
* 0-50执行的是主线程,50-100执行的是A线程,并且将A线程完全执行完后才继续执行主线程
*/
public class ThreadDemo{
public static void main(String[] args) {
MyThread t = new MyThread("A");
t.start();
for (int i = 0; i < 100; i++) {
if (i>50) {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("主线程"+"["+i+"]");
}
}
}
下面是Java Platform SE8 API中对Thread中Join方法的解释:
public final void join(long millis)
throws InterruptedExceptionWaits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
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. Parameters:
millis - the time to wait in milliseconds
Throws:
IllegalArgumentException - if the value of millis is negative
InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
先上代码
新建一个Thread,代码如下:
package com.thread.test;
public class MyThread extends Thread {
private String name;
public MyThread(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(name+"["+i+"]");
}
super.run();
}
}
之后新建测试类,代码如下:
package com.thread.test;
/*
* 0-50执行的是主线程,50-100执行的是A线程,并且将A线程完全执行完后才继续执行主线程
*/
public class ThreadDemo{
public static void main(String[] args) {
MyThread t = new MyThread("A");
t.start();
for (int i = 0; i < 100; i++) {
if (i>50) {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("主线程"+"["+i+"]");
}
}
}
下面是Java Platform SE8 API中对Thread中Join方法的解释:
public final void join(long millis)
throws InterruptedExceptionWaits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
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. Parameters:
millis - the time to wait in milliseconds
Throws:
IllegalArgumentException - if the value of millis is negative
InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
我自己的理解就是会强行进入使用join方法的线程,其他线程等待该线程完全执行完后才会进来。
浅谈Java多线程中的join方法的更多相关文章
- Java多线程中的join()方法
一.join()方法介绍 join() 定义在Thread.java中.join()方法把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A的join( ...
- Java多线程中的join方法
新建一个Thread,代码如下: package com.thread.test; public class MyThread extends Thread { private String name ...
- java多线程中关于join方法的使用
Thread的非静态方法join()让一个线程B"加入"到另外一个线程A的尾部.在A执行完毕之前,B不能工作.例如: Thread t = new MyThread ...
- Java并发编程--多线程中的join方法详解
Java Thread中, join()方法主要是让调用该方法的thread在完成run方法里面的部分后, 再执行join()方法后面的代码 例如:定义一个People类,run方法是输出姓名年龄. ...
- 浅谈JAVA GUI中,AWT与Swing的区别、联系及优缺点
浅谈JAVA GUI中,AWT与Swing的区别.联系及优缺点 A.区别 1.发布的时间 AWT是在JDK 1.0版本时提出的 Swing是在AWT之后提出的(JAVA 2) 2. ”重量” AWT是 ...
- java 多线程中的wait方法的详解
java多线程中的实现方式存在两种: 方式一:使用继承方式 例如: PersonTest extends Thread{ String name; public PersonTest(String n ...
- 【JAVA多线程中使用的方法】
一.sleep和wait的区别. 1.wait可以指定时间,也可以不指定. 而sleep必须制定. 2.在同步的时候,对于CPU的执行权和以及锁的处理不同. wait:释放执行权,释放锁. sleep ...
- 模拟做饭系统(java+线程中的join方法)
(一)项目框架分析 妈妈要去做饭,发现没有酱油,让儿子去买酱油,然后回来做饭. 根据面向对象的思想,有两个对象,妈妈和儿子 主要有两个方法: (一)没有线程控制(即儿子没有买酱油回来妈妈就做好饭了)+ ...
- 浅谈Java多线程
线程与进程 什么是进程? 当一个程序进入内存中运行起来它就变为一个进程.因此,进程就是一个处于运行状态的程序.同时进程具有独立功能,进程是操作系统进行资源分配和调度的独立单位. 什么是线程? 线程是进 ...
随机推荐
- Netty 发送消息失败或者接收消息失败的可能原因
1. 消息发送失败: 检查通道是否建立成功 Netty中的通道建立采用的是异步方式,获取到的通道对象可能为空或初始化未完成: 2. 接收的消息有丢失 消息可能会粘包,是否有拆包机制
- Linux - history命令的常用方法
history命令 打印所有命令记录:history 打印最近10条记录:history 10 执行第123条命令记录:!123 重复执行上一条命令:!! 执行最后一次以ls开头的命令:!ls 逐屏列 ...
- Python集成开发工具Pycharm的使用方法:复制,撤销上一步....
复制行,在代码行光标后,输入Ctrl + d ,即为复制一行,输入多次即为复制多行 撤销上一步操作:Ctrl + z 为多行代码加注释# 代码选中的条件下,同时按住 Ctrl+/,被选中行被注释,再 ...
- vue环境安装
node.js安装 https://nodejs.org/en/ cnpm安装 npm install -g cnpm --registry=https://registry.npm.taobao.o ...
- odoo开发笔记 -- 进入后台调试模式
./odoo-bin shell -d test1 -c /home/odoo/odooshare/odoo.conf ./odoo-bin shell -d 数据库名 -c 指定配置文件
- 高手速成android开源项目【developer篇】
主要介绍和Android开发工具和测试工具相关的开源项目. Buckfacebook开源的Android编译工具,效率是ant的两倍.主要优点在于:(1) 加快编译速度,通过并行利用多核cpu和跟踪不 ...
- zmq Poller
1.注册socket到poller中 poller = zmq.Poller() poller.register(frontend, zmq.POLLIN) # receive worker mess ...
- Makefile.am文件配置
Makefile.am Makefile.am是一种比Makefile更高层次的编译规则,可以和configure.in文件一起通过调用automake命令,生成Makefile.in文件,再调用./ ...
- angular监听dom渲染完成,判断ng-repeat循环完成
一.前言 最近做了一个图片懒加载的小插件,功能需要dom渲染完成后,好获取那些需要懒加载的dom元素.那么问题来了,如果只是感知静态的dom用ready,onload都可以,但项目用的angular, ...
- Animate.css(一款有意思的CSS3动画库)
官网:https://daneden.github.io/animate.css/ animate.css 是一款跨浏览器的动画库. 使用方式: 在页面的 <head>中引入样式文件: & ...