多线程:head first Thread.join()
不使用Thread.join() 测试线程
先上代码:
/**
* Created by Zero on 2017/8/23.
*/
public class TestJoin implements Runnable {
public static int a = 0;
@Override
public void run() {
for (int i = 0; i < 5; i++) {
a = a + 1;
}
}
public static void main(String[] args) throws InterruptedException {
TestJoin j = new TestJoin();
Thread thread = new Thread(j);
thread.start();
System.out.println(a);
}
}
以上示例会输出5吗?可能性不大,有可能永远输出为0,之前在线程池原理的那篇就提到过,线程的启动和销毁都需要时间,此处因为thread还没启动好,或者正在为它分配资源准备运行,就已经执行完输出了。
怎样才能确保每次都能输出5呢?现在有请我们的主角join方法闪亮登场,代码如下:
/**
* Created by apple on 2017/8/23.
*/
public class TestJoin implements Runnable {
public static int a = 0;
@Override
public void run() {
for (int i = 0; i < 5; i++) {
a = a + 1;
}
}
public static void main(String[] args) throws InterruptedException {
TestJoin j = new TestJoin();
Thread thread = new Thread(j);
thread.start();
/**
* 测试join方法的作用,与下面的threadAgain线程作对比。
*/
thread.join();
System.out.println(a);
a = 0;
Thread threadAgain = new Thread(j);
threadAgain.start();
System.out.println(a);
}
}
输出的结果将是5和0。
Thread.join()作用
Thread.join(),之前看资料的时候,有些人说可以理解成“将两个线程合并成一个线程”,我是觉得这样说是很不科学的,虽然这样通俗易懂,但这确实是两个不同的线程,只是在调用Thread.join()后,会先执行完Thread线程后再去执行当前线程,即上述的在主线程中执行到thread.join();后,先去执行thread,直到thread执行完后再去执行主线程。
测试Thread.join(long millis)
/**
* Created by apple on 2017/8/23.
*/
public class TestJoin implements Runnable {
public static int a = 0;
@Override
public void run() {
for (int i = 0; i < 5; i++) {
a = a + 1;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
TestJoin j = new TestJoin();
Thread thread = new Thread(j);
thread.start();
/**
* 测试join方法的作用
*/
thread.join(3000);
System.out.println("thread线程结果为:"+a);
a = 0;
Thread threadAgain = new Thread(j);
threadAgain.start();
System.out.println("threadAgain线程结果为:"+a);
}
}
输出:
thread线程结果为:3
threadAgain线程结果为:0
先上一段源码再来分析:
/**
* Waits at most {@code millis} milliseconds for this thread to
* die. A timeout of {@code 0} means to wait forever.
*
* <p> This implementation uses a loop of {@code this.wait} calls
* conditioned on {@code this.isAlive}. As a thread terminates the
* {@code this.notifyAll} method is invoked. It is recommended that
* applications not use {@code wait}, {@code notify}, or
* {@code notifyAll} on {@code Thread} instances.
*
* @param millis
* the time to wait in milliseconds
*
* @throws IllegalArgumentException
* if the value of {@code millis} is negative
*
* @throws InterruptedException
* if any thread has interrupted the current thread. The
* <i>interrupted status</i> of the current thread is
* cleared when this exception is thrown.
*/
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
源码爸爸说了,孩子,我给你millis这么长的时间,能不能完成任务那是你的事情了,能提前完成,咱就提前走下去,不能完成,过期不候,自己看着办吧。
默认情况下,Thread.join()即Thread.join(0),当为0的时候,那才叫真爱呢,线程会一直等下去,知道执行结束为止,才会继续朝下执行。
isAlive():用来测试线程是否处于活动状态,相当于 run 是否还在执行。
多线程:head first Thread.join()的更多相关文章
- 多线程进阶---Thread.join()/CountDownLatch.await() /CyclicBarrier.await()
Thread.join() CountDownLatch.await() CyclicBarrier.await() 三者都是用来控制程序的"流动" 可以让程序"堵塞&q ...
- java多线程同步以及线程间通信详解&消费者生产者模式&死锁&Thread.join()(多线程编程之二)
本篇我们将讨论以下知识点: 1.线程同步问题的产生 什么是线程同步问题,我们先来看一段卖票系统的代码,然后再分析这个问题: package com.zejian.test; /** * @author ...
- C#多线程详解(一) Thread.Join()的详解
bicabo C#多线程详解(一) Thread.Join()的详解 什么是进程?当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程 ...
- 多线程编程(一) - 关于C#中Thread.Join()
Thread.Join()在MSDN中的解释很模糊:Blocks the calling thread until a thread terminates 有两个主要问题:1.什么是the calli ...
- C#多线程Thread.Join()的详解
class TestThread { private static void FirstThreadFun() { ; i < ; i++) { Console.WriteLine(Thread ...
- 多线程--Thread.join方法
在Thread类的Api中,Join的作用是让当前线程等待目标线程结束之后才继续执行. thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程. 比如在线程B ...
- java 多线程 Thread.join子线程结束父线程再运行;join(long):等待超时毫秒数
Join的使用 目的:当子线程运行结束后,父线程才能再继续运行 /** * @ClassName ThreadJoinExample * @projectName: object1 * @author ...
- C#中Thread.Join()的理解
最近在项目中使用多线程,但是对多线程的一些用法和概念还有有些模棱两可,为了搞清楚查阅了一写资料,写下这篇日志加深理解吧. Thread.Join()在MSDN中的解释很模糊:Blocks the ca ...
- Java Thread.join的作用和原理
很多人对Thread.join的作用以及实现了解得很少,毕竟这个api我们很少使用.这篇文章仍然会结合使用及原理进行深度分析 内容导航 Thread.join的作用 Thread.join的实现原理 ...
随机推荐
- 【每天一道算法题】Lucky String
A string s is LUCKY if and only if the number of different characters in s is a fibonacci number. Gi ...
- Zepto源码分析(一)核心代码分析
本文只分析核心的部分代码,并且在这部分代码有删减,但是不影响代码的正常运行. 目录 * 用闭包封装Zepto * 开始处理细节 * 正式处理数据(获取选择器选择的DOM) * 正式处理数据(添加DOM ...
- 自定义EasyUI的datetimebox控件日期时间的显示格式(转)
工作中遇到的问题,在此记录一下. 需求:前台页面使用了EasyUI框架,在某一个html页面中要求datetimebox显示格式为年月日和小时,如图所示: 尝试过两种方法,分别如下: 第一种方法: d ...
- 使用oschina的git服务器图文流程 (转)
参考了豆沙包的教程我自己也做了一遍也来写写我的心得和体会 由于报名参加了游戏蛮牛<刀塔传奇>开源项目,服务器+客户端,所以觉着不管时间怎么着,还是或多或少做点贡献吧.毕竟这种体验应该还是第 ...
- Win10家庭版重命名Administrator用户文件夹
需要将Windows系统默认的Administrator帐号文件夹改名为我们自定义的名称.. 但是Win10家庭版找不到组策略gpedit.msc 根据微软官方有关Win10各版本操作系统中对于组策略 ...
- Java项目访问resources文件
最近在对接支付宝支付的开发,需要取到支付的RSA公钥和私钥.于是把公钥和私钥加到resources文件夹里.但是不知道怎么读到这两个文件,也就是不知道路径怎么写.于是网上搜索了下如何获取工作路径,Sy ...
- asp .net 模板引擎 使用 Razor 生成html静态页面
刚开始不是理解 写完之后 觉得还蛮简单的 分为这几个步骤 1.获取页面模板Html 2.获取数据 3.解析模板和数据,生成静态页Html代码 4.生成静态文件 模板形式是mvc的模式,会mvc 看一下 ...
- c++中string.erase()函数的用法(转)
erase函数的原型如下:(1)string& erase ( size_t pos = 0, size_t n = npos );(2)iterator erase ( iterator p ...
- jquery的2.0.3版本源码系列(4):285-348行,extend方法详解
目录 1 . jquery extend的基本使用 通过285行的源码 jQuery.extend = jQuery.fn.extend = function() { ,extend方法要么是直接挂在 ...
- webpack教程(六)——分离组件代码
先来运行一下webpack命令, 看到app.js才4k. 安装一下react npm install react --sava-dev 在app/index.js文件内引入react 运行webpa ...