浅谈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多线程
线程与进程 什么是进程? 当一个程序进入内存中运行起来它就变为一个进程.因此,进程就是一个处于运行状态的程序.同时进程具有独立功能,进程是操作系统进行资源分配和调度的独立单位. 什么是线程? 线程是进 ...
随机推荐
- [CocoaPods]使用Pod Lib创建
入门 我们将使用pod lib create引导过程来创建整个pod .那么让我们从初始命令开始: pod lib create MyLibrary 注意:要使用您自己的pod-template,您可 ...
- vue 项目记录.路飞学城(一)
前情提要: 通过vue 搭建路飞学城记录 一:项目分析 二:项目搭建 1:创建项目 vue init webpack luffy 2:初始化项目 清除默认的HelloWorld.vue组件和APP. ...
- leetcode — median-of-two-sorted-arrays
import java.util.HashSet; import java.util.Set; /** * Source : https://oj.leetcode.com/problems/long ...
- 第一个Quartz程序 (二)
1 我们使用maven项目 2 创建一个job类,在execute()方法里写上业务逻辑代码. 3 在另外一个类中创建触发器,调度器,并且绑定job. 首先在项目的pom.xml引入需要的jar包. ...
- (转)创建GitHub技术博客
https://blog.csdn.net/renfufei/article/details/37725057
- spring的第一天
spring的第一天 ssm框架 spring Spring是什么? Spring是容器框架,用来配置(装)Bean,并且维护Bean之间的关系.其中Bean可以是Java中的任何一种对象,可以是J ...
- 深入React技术栈之初入React世界
1.1 react简介 react并不是完整的MVM/MVVM框架,专注于提供清晰.简洁的View层解决方案. 传统开发模式,要更新页面需要手动操作DOM元素.如图1.1所示,React在DOM上封装 ...
- [转]JS实现千分位
本文转自:https://www.cnblogs.com/lvmylife/p/8287247.html 方法一:正则实现 function format (num) { var reg=/\d{1, ...
- MailBee.NET
MailBee.NET Objects 是一款为创建.发送.接收以及处理电子邮件而设计的健壮.功能丰富的.NET控件.具备“必需”以及独特的功能,这些控件帮助开发人员简单快速地将复杂的电子邮件功能添加 ...
- ExtJS中xtype一览
基本组件: xtype Class 描述 button Ext.Button 按钮 splitbutton Ext.SplitButton 带下拉菜单的按钮 cycle Ext.CycleButton ...