Java多线程中关于join方法的使用实例解析
作者:52Hz 发布时间:2023-08-22 21:47:29
标签: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方法的线程,其他线程等待该线程完全执行完后才会进来。
来源:http://www.cnblogs.com/Miracle-Maker/archive/2017/01/08/6261738.html


猜你喜欢
- C#在程序中定义和使用自定义事件可以分为以下几个步骤:步骤1:在类中定义事件using System;public class TestCl
- 第一种方法:获取手机的IMSI码,并判断是中国移动\中国联通\中国电信TelephonyManager telManager = (Tele
- 摘要:本文介绍Linq查询基本操作(查询关键字)- from 子句- where 子句- select子句- group 子句- into
- 一、什么是状态模式定义:当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。主要解决:当控制一个对象状态的条件表达式过于
- 本文实例讲述了C#动态执行批处理命令的方法。分享给大家供大家参考。具体方法如下:C# 动态执行一系列控制台命令,并允许实时显示出来执行结果时
- 在进行详解之前,我想先声明一下,本次我们进行讲解说明的是 Kafka 消息存储的信息文件内容,不是所谓的 Kafka 服务器运行产生的日志文
- 背景工作中遇到业务诉求是通过OpenCV对图片进行一些判断操作和优化,这里是看了部分不错的文章,希望总结一个自己的学习过程,温故而知新,有不
- 经过几天的折腾,终于到了学习一个重量级的查询方式上,使用@Query注解,使用注解有两种方式,一种是JPQL的SQL语言方式,一种是原生SQ
- 静态库和动态库的区别1、静态库的扩展名一般为".a"或者".lib";动态库的扩展名一般为"
- 大家好,这是 [C#.NET 拾遗补漏] 系列的第 07 篇文章。在 C# 中,大多数方法都是通过 return 语句立即把程序的控制权交回
- 本文实例讲述了java编程实现根据EXCEL列名求其索引的方法。分享给大家供大家参考,具体如下:原理:[a1-z26]*26^n-1 + [
- 前言通过深入分析Spring源码,我们知道Spring框架包括大致六大模块, 如Web模块,数据库访问技术模块,面向切面模块,基础设施模块,
- 本文实例讲述了C#静态static的用法,分享给大家供大家参考。具体用法分析如下:一、静态类静态类与非静态类的重要区别在于静态类不能实例化,
- 最近在做一个移动端HTML5的应用,使用到了上传功能,起初使用传统的上传方式上传手机拍照的照片,由于手机拍照出来的照片一般都是好几MB,所以
- 前言在淘宝内网里看到同事发了贴说了一个CPU被100%的线上故障,并且这个事发生了很多次,原因是在Java语言在并 * 况下使用HashMap
- 前言记一次为了节省代码没有在方法体中声明HttpServletRequest,而用autowire直接注入所钻的坑结论:给心急的人。 直接在
- 本文实例讲述了Java Web项目部署在Tomcat运行出错与解决方法。分享给大家供大家参考,具体如下:1、在部署Java Web项目的过程
- 包括了写入和读取功能. 写入的时候, 如果文件不存在会自动创建. 如果对应的键已经存在, 则自动覆盖它的值. 读取的时候, 如果对应的文件不
- 本文实例为大家分享了OpenCV实现直线检测并消除的具体代码,供大家参考,具体内容如下很简单,代码如下#include<iostrea
- 1. 初始 Spring Boot1.1 什么是Spring BootSpring 的诞生是为了简化 Java 程序的开发的Spring B