java线程挂起(join)
来源:互联网 发布:数据统计分析的作用 编辑:程序博客网 时间:2024/06/07 15:51
1、线程挂起join简介
将另外一个线程join到当前线程,则需要等到join进来的线程执行完才会继续执行当前线程。
thread.join(); //当前线程挂起,调用线程 thread,在thread线程执行完毕之前,当前线程一直挂起不执行。
thread.join(1000); //当前线程挂起,调用线程 thread,等待 thread 线程,等待时间是1000毫秒,超过这个时间,当前线程进入可运行状态。
2、代码事例
package com.xxx.util;import java.util.Date;import java.util.concurrent.TimeUnit;/** * Created with IntelliJ IDEA. * Date: 15-3-27 * Time: 上午9:38 * To change this template use File | Settings | File Templates. */public class ThreadJoin1 implements Runnable{ @Override public void run() { System.out.printf("Beginning threadJoin1 loading:%s\n",new Date()); try { TimeUnit.SECONDS.sleep(6); } catch (InterruptedException e) { System.out.println("ThreadJoin1 has bean interrupted"); } System.out.println("ThreadJoin1 loading has finished."); }}
package com.xxx.util;import java.util.Date;import java.util.concurrent.TimeUnit;/** * Created with IntelliJ IDEA. * Date: 15-3-27 * Time: 上午9:38 * To change this template use File | Settings | File Templates. */public class ThreadJoin2 implements Runnable{ @Override public void run() { System.out.printf("Beginning threadJoin2 loading:%s\n",new Date()); try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) { System.out.println("ThreadJoin2 has bean interrupted"); } System.out.println("ThreadJoin2 loading has finished."); }}main类
package com.xxx.util;import java.util.Date;/** * Created with IntelliJ IDEA. * Date: 15-3-27 * Time: 上午9:44 * To change this template use File | Settings | File Templates. */public class ThreadJoinMain { public static void main(String[] args){ Thread threadJoin1 = new Thread(new ThreadJoin1()); Thread threadJoin2 = new Thread(new ThreadJoin2()); threadJoin1.start(); threadJoin2.start(); try { //main线程等待threadJoin1线程1秒,超过一秒,main线程进入可运行状态 threadJoin1.join(1000); threadJoin2.join(); } catch (InterruptedException e) { System.out.println("Handle InterruptedException."); } System.out.printf("%s:Configuration has bean loaded:%s\n",Thread.currentThread().getName(),new Date()); }}
3、运行结果
当前线程挂起等待。
0 0
- java线程挂起(join)
- 线程的挂起join
- java线程等待、设置优先级、同步、挂起、恢复(Join、setPriority、synchronized、wait、notify……)
- java线程等待、设置优先级、同步、挂起、恢复(Join、setPriority、synchronized、wait、notify……)
- Java多线程(三)——线程挂起、恢复、终止
- java线程中的join
- JAVA 线程 join
- java 线程 join
- [Java] 线程之join
- java线程之join
- java线程join
- Java线程join方法
- java 线程合并(join)
- java线程 join
- Java线程之join()
- Java 之 线程join
- java基础线程-join()
- java 线程 join
- 简单中英文排序
- UIImageView不显示问题
- <linux-sed> sed基本用法
- 本地缓存工具类
- 算法之归并排序
- java线程挂起(join)
- javac和java用法备忘
- ASP.NET MVC使用jQuery来POST数据至数据库中
- IOS开发UI篇—导航控制器属性和基本使用
- Oracle11g无法登录DatabaseControl浏览器
- JAVA wait(), notify(),sleep详解
- 理解 Github Flow
- iOS截图
- Mybatis 复习 动态SQL