Mark Knowledge of Java Thread (1): Join Method
2015-08-07 15:17
549 查看
Join is A Simple Way
Assuming a scenario of mulit-threads, there are two threads(tA and tB). How can we insure that tB must be finished before tA is finished? A simple way is to let tB joint tA so Thread.join() is convenience.Join Talk to You
There are three join methods which are defined in thread class.- public final void join() throws InterruptedException - public final synchronized void join(long millis) throws InterruptedException - public final synchronized void join(long millis, int nanos) throws InterruptedException
Let Me Try
For show its ability of this function. I design a thread flow to show it as blew. Each thread prints some message so that we can check the programe excute order.Created with Raphaël 2.1.0Join Thread DemoMainThreadMainThreadThread1Thread1Thread2Thread2start thread 1join thread 1start thread 2invoke join() for thread 2do something.EndNotify thread 2 is finish to other threads.Main Thread still blocks because thread 1 hasn't finished.Thread 1 can run after thread 2 has finished.Do sometingEndNotify thread 1 is finish to other threads.Main thread can run after that.Do somethingEndPrograme end.
/** * Simple way to let t1 stop before t2 * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread() { public void run() { System.out.println("Thread 1 start."); Thread t2 = new Thread() { public void run() { System.out.println("Thread 2 start."); try { //Simulate t2 do a long action System.out.println("Thread 2 is busy."); Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread 2 finish."); } }; t2.start(); try { t2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread 1 finish."); } }; t1.start(); t1.join(); System.out.println("Main thread finish."); } }
Welcome to My Home
In resource code, there is a while loop for check thread status. If thread’s status is alive ( isAlive() ), it will invoke wait() method to wait a few time until other thread call notify function or inter/** * 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; } } }
相关文章推荐
- Could not lock surface java.lang.IllegalArgumentException
- JAVA正则表达式语法大全
- Java——高级特征
- 返回的bean的属性为空不显示的spring配置
- 全面分析 Spring 的编程式事务管理及声明式事务管理
- 全面分析 Spring 的编程式事务管理及声明式事务管理
- 全面分析 Spring 的编程式事务管理及声明式事务管理
- 全面分析 Spring 的编程式事务管理及声明式事务管理
- 全面分析 Spring 的编程式事务管理及声明式事务管理
- Java设计模式—生产者消费者模式(阻塞队列实现)
- spring AOP 最佳实践
- Eclipse 常用快捷键 (动画讲解)
- Java:类与继承
- 【实验室】建立springMVC的简单项目
- Java 执行引擎(从字节码到机器码)
- 使用java.util.concurrent.ExecutorService实现Java线程异步执行
- Java学习之eclipse
- Java报错异常-----Exception in thread "main" java.lang.NullPointerException
- java swing弹框显示问题
- 指定eclipse启动时使用的jdk版本(在64位操作系统中同时运行64位eclipse和32位eclipse)