先上代码

新建一个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方法的使用实例解析相关推荐

  1. java商城项目中多线程执行_java多线程中执行多个程序的实例分析

    我们知道多线程因为同时处理子线程的能力,对于程序运行来说,能够达到很高的效率.不过很多人对于多线程的执行方法还没有尝试过,本篇我们将为大家介绍创建线程的方法,在这个基础上,对程序执行多条命令的方法进行 ...

  2. java 如何添加背景音乐_Java JFrame 中添加背景音乐方法

    在想让音乐开始播放的时候 调用该方法就可以,如:如果想在一打开JFrame的时候就播放,那么在JFrame的构造方法中添加该方法即可.以此类推. import java.applet.AudioCli ...

  3. java 线程 while循环_java多线程中while循环的问题

    public class Test{ public static void main(String[] args) { try { MyThread th = new MyThread(); th.s ...

  4. python中什么是实例-在Python中使用’__rsub__’方法的典型实例是什么?

    基本示例.您编写自己的类似int的类: class FooInt: ... other stuff elided ... def __sub__(self, other): if isinstance ...

  5. java多线程中的join方法详解

    java多线程中的join方法详解 方法Join是干啥用的? 简单回答,同步,如何同步? 怎么实现的? 下面将逐个回答. 自从接触Java多线程,一直对Join理解不了.JDK是这样说的:join p ...

  6. vue 给标签添加data属性_在Vue中获取自定义属性方法:data-id的实例

    获取自定义属性的方法: 第一步:首先在标签上绑定上@click="getDateId(item.id)",并将属性值传到绑定的事件里面 第二步:在标签上继续绑定:date-id = ...

  7. python中property魔法方法原理_Python类中的魔法方法之 __slots__原理解析

    在类中每次实例化一个对象都会生产一个字典来保存一个对象的所有的实例属性,这样非常的有用处,可以使我们任意的去设置新的属性. 每次实例化一个对象python都会分配一个固定大小内存的字典来保存属性,如果 ...

  8. java中thread实例_Java多线程2:Thread中的实例方法

    Thread类中的方法调用方式: 学习Thread类中的方法是学习多线程的第一步.在学习多线程之前特别提出一点,调用Thread中的方法的时候,在线程类中,有两种方式,一定要理解这两种方式的区别: 1 ...

  9. java闭合数据_java多线程中线程封闭详解

    线程封闭的概念 访问共享变量时,通常要使用同步,所以避免使用同步的方法就是减少共享数据的使用,这种技术就是线程封闭. 实现线程封闭的方法 1:ad-hoc线程封闭 这是完全靠实现者控制的线程封闭,他的 ...

最新文章

  1. 测试晶面间距软件_丽江导电橡胶电阻率测试仪生产商
  2. 2018年东北农业大学春季校赛 D wyh的迷宫【搜索】
  3. 加入/ LINQ和Lambda的位置
  4. java如何使显示字符下标_Java如何在 Word 中设置上、下标
  5. python socket send_python socket 连续send,出现粘包问题
  6. 客户关系管理OACRM商品进销存销售管理合同订单库存财务管理跟单PHP源码
  7. mysql数据表案例_mysql中库和表的简单操作案例
  8. 商务专业考计算机二级,计算机二级ms考什么
  9. linux+udp数据传输,Linux利用socket实现两台pc之间的数据传输功能,包括windows到linux,UDP实现...
  10. [zt] HTTP 协议及其POST与GET操作差异 C#中如何使用POST、GET等
  11. eclipse各版本对应JDK版本要求
  12. FFT算法的C语言实现
  13. Excel小技巧分享-自动缩放的表格来了
  14. 计算机标准体重计算公式,项目2:就拿胖子说事---(2)计算出标准体重,当超重时,请给出提示...
  15. [c/c++]trivial/POD类型和standard layout
  16. IPv4/IPv6、DHCP、网关、路由
  17. cmd打开常用设置以及应用的快捷方法
  18. c语言设计简单计算器,c语言设计简单计算器实现加减乘除运算
  19. Windows 平台下局域网劫持测试工具 – EvilFoca
  20. 【u-boot】u-boot-2016.09 make编译过程分析(二)

热门文章

  1. 奇异值分解讨论及其实现的计算步骤
  2. Python学习笔记:异步IO(1)
  3. Canny边缘检测算法
  4. 云炬Android开发笔记 10主界面-首页UI与数据解析器开发(RecyclerView)
  5. 第17课:循环神经网络(RNN)
  6. PIC单片机入门_异步通讯模式详解
  7. oop中构造函数编写的注意事项
  8. go语言中使用递归函数实现文件目录的遍历
  9. static、final、abstract基本作用
  10. 一些概念整理(不一定完全正确)