先上代码

新建一个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方法的更多相关文章

  1. Java多线程中的join()方法

    一.join()方法介绍 join() 定义在Thread.java中.join()方法把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A的join( ...

  2. Java多线程中的join方法

    新建一个Thread,代码如下: package com.thread.test; public class MyThread extends Thread { private String name ...

  3. java多线程中关于join方法的使用

    Thread的非静态方法join()让一个线程B"加入"到另外一个线程A的尾部.在A执行完毕之前,B不能工作.例如:         Thread t = new MyThread ...

  4. Java并发编程--多线程中的join方法详解

    Java Thread中, join()方法主要是让调用该方法的thread在完成run方法里面的部分后, 再执行join()方法后面的代码 例如:定义一个People类,run方法是输出姓名年龄. ...

  5. 浅谈JAVA GUI中,AWT与Swing的区别、联系及优缺点

    浅谈JAVA GUI中,AWT与Swing的区别.联系及优缺点 A.区别 1.发布的时间 AWT是在JDK 1.0版本时提出的 Swing是在AWT之后提出的(JAVA 2) 2. ”重量” AWT是 ...

  6. java 多线程中的wait方法的详解

    java多线程中的实现方式存在两种: 方式一:使用继承方式 例如: PersonTest extends Thread{ String name; public PersonTest(String n ...

  7. 【JAVA多线程中使用的方法】

    一.sleep和wait的区别. 1.wait可以指定时间,也可以不指定. 而sleep必须制定. 2.在同步的时候,对于CPU的执行权和以及锁的处理不同. wait:释放执行权,释放锁. sleep ...

  8. 模拟做饭系统(java+线程中的join方法)

    (一)项目框架分析 妈妈要去做饭,发现没有酱油,让儿子去买酱油,然后回来做饭. 根据面向对象的思想,有两个对象,妈妈和儿子 主要有两个方法: (一)没有线程控制(即儿子没有买酱油回来妈妈就做好饭了)+ ...

  9. 浅谈Java多线程

    线程与进程 什么是进程? 当一个程序进入内存中运行起来它就变为一个进程.因此,进程就是一个处于运行状态的程序.同时进程具有独立功能,进程是操作系统进行资源分配和调度的独立单位. 什么是线程? 线程是进 ...

随机推荐

  1. 使用diskpart命令格式化U盘

    在安装各种linux系统时,经常要使用SD卡,这里就出了个问题,就是SD卡被linux格式化之后无法在widdows下使用. 并且windows下的格式化按键对它毫无作用.笔者在摸索的过程中,找到了格 ...

  2. js中break、continue和return的一般用法总结

    break break :终止break的整个循环体,包括内部所有循环.但对循环体外部的循环不影响. for(let i = 0;i<2;i++){ for(let j = 0;j<2;j ...

  3. [原创]Struts2奇葩环境任意文件上传工具(解决菜刀无法传文件或上传乱码等问题)

    上面这问题问得好  1 不知道大家有没碰到有些Strus2站点  上传JSP后访问404 或者503    注意我说的是404或503不是403(要是403换个css/img等目录或许可以)    但 ...

  4. Android多线程编程<一>Android中启动子线程的方法

          我们知道在Android中,要更新UI只能在UI主线程去更新,而不允许在子线程直接去操作UI,但是很多时候,很多耗时的工作都交给子线程去实现,当子线程执行完这些耗时的工作后,我们希望去修改 ...

  5. Centos7安装Mysql8(官方整合包)

    1. 下载整合包 [root@master ~]# wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.12-1.el7.x86_ ...

  6. Jenkins CLI 命令详解

    笔者在前文<通过 CLI 管理 Jenkins Server>中介绍了如何通过 SSH 或客户端命令行的方式管理 Jenkins Server,限于篇幅,前文主要的目的是介绍连接 Jenk ...

  7. 使用BizTalk实现RosettaNet B2B So Easy

    使用BizTalk实现RosettaNet B2B So Easy 最近完成了一个vmi-hub的B2B项目,使用Rosettanet 2.0的标准与一家品牌商,OEM,供应商实现B2B.一共交换4个 ...

  8. springboot+cloud 学习(三)消息中间件 RibbitMQ+Stream

    安装RabbitMQ window下安装: (1):下载erlang,原因在于RabbitMQ服务端代码是使用并发式语言erlang编写的,下载地址:http://www.erlang.org/dow ...

  9. 【图文详细教程】maven3安装配置+eclipse离线安装maven3插件《《唯一成功的教程~~~2018-01-09》》

    环境搭建前提: 1.电脑上已经安装了1.7以及以上版本的JDK(因为我提供的maven版本是最新的3.3.9的,要求最低JDK1.7) 2.配置好了ecplise并且能正常启动 第一步:下载maven ...

  10. Jmeter配置通过SSH连接的mysql数据库

    jmeter连接配置mysql数据库时,如果数据库服务器没有通过ssh连接,则只需要添加配置相应的jdbc参数就可以了.如果数据库服务器是通过SSH连接的,则需要通过中间远程连接工具来登录,此处使用的 ...