Java线程中的join方法
2016-11-26 10:56
399 查看
join是Thread类的一个方法,启动线程后直接调用,例如:
为什么要用join()方法,它会为我们解决什么问题呢?
在很多情况下,有运行时间很长的逻辑与计算任务的时候,我们会启动一个或多个子线程去进行处理,这时我们有以下几种情况会出现:
后续的处理中不需要等待子线程处理的结束,也不关心子线程是否执行完成。这时我们可以直接运行我们主线程直到结束。
我们需要等待子线程全部执行结束,我们才可以往下执行主线程。
在主线程的后续逻辑处理中需要子线程中的处理结果。
我们先看第一种情况:
运行结果:主线程直接运行结果,这时在主线程的运行过程中我们不关心子线程是否运行成功,也不关心子线程的执行结果。
第二种情况:
运行结果:这时,我们不关心子线程的运行结果,但是我们需要等待子线程运行结束后,主线程才可以运行接下来的逻辑。
第三种情况:
运行结果:这时我们需要等待子线程运程结束,并将运行结果返回,如果在主线程需要使用,我们就可以直接使用了。
PS:技术有限,如有不对之处,欢迎大家指出。
Thread t = new AThread(); t.start(); t.join();
为什么要用join()方法,它会为我们解决什么问题呢?
在很多情况下,有运行时间很长的逻辑与计算任务的时候,我们会启动一个或多个子线程去进行处理,这时我们有以下几种情况会出现:
后续的处理中不需要等待子线程处理的结束,也不关心子线程是否执行完成。这时我们可以直接运行我们主线程直到结束。
我们需要等待子线程全部执行结束,我们才可以往下执行主线程。
在主线程的后续逻辑处理中需要子线程中的处理结果。
我们先看第一种情况:
public class AThread extends Thread{ @Override public void run() { System.out.println("This aThread is running ..." + this.getName()); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("This aThread is end ..." + this.getName()); } }
public class BThread extends Thread{ @Override public void run() { System.out.println("This bThread is running ..." + this.getName()); try { sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("This bThread is end ..." + this.getName()); } }
public class Demo { public static void main(String[] args) throws InterruptedException { long startTime = System.currentTimeMillis(); Thread aThread = new AThread(); Thread bThread = new BThread(); aThread.start(); bThread.start(); Long endTime = System.currentTimeMillis(); System.out.println("Main mothed is end,use time :" + (endTime - startTime)); } }
运行结果:主线程直接运行结果,这时在主线程的运行过程中我们不关心子线程是否运行成功,也不关心子线程的执行结果。
第二种情况:
public class Demo { public static void main(String[] args) throws InterruptedException { long startTime = System.currentTimeMillis(); Thread aThread = new AThread(); Thread bThread = new BThread(); aThread.start(); bThread.start(); //我们需要等待子线程结束后,再进行主线程 aThread.join(); bThread.join(); Long endTime = System.currentTimeMillis(); System.out.println("Main mothed is end,use time :" + (endTime - startTime)); } }
运行结果:这时,我们不关心子线程的运行结果,但是我们需要等待子线程运行结束后,主线程才可以运行接下来的逻辑。
第三种情况:
public class AThread extends Thread{ private String resultA; @Override public void run() { System.out.println("This aThread is running ..." + this.getName()); try { sleep(1000); this.resultA = "AThread rsult is a"; } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("This aThread is end ..." + this.getName()); } public String getResultA() { return resultA; } public void setResultA(String resultA) { this.resultA = resultA; } }
public class BThread extends Thread{ private String resultB; @Override public void run() { System.out.println("This bThread is running ..." + this.getName()); try { sleep(2000); this.resultB = "BThread rsult is b"; } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("This bThread is end ..." + this.getName()); } public String getResultB() { return resultB; } public void setResultB(String resultB) { this.resultB = resultB; } }
public class Demo { public static void main(String[] args) throws InterruptedException { long startTime = System.currentTimeMillis(); AThread aThread = new AThread(); BThread bThread = new BThread(); aThread.start(); bThread.start(); //我们需要等待子线程结束后,再进行主线程 aThread.join(); System.out.println(aThread.getResultA()); bThread.join(); System.out.println(bThread.getResultB()); Long endTime = System.currentTimeMillis(); System.out.println("Main mothed is end,use time :" + (endTime - startTime)); } }
运行结果:这时我们需要等待子线程运程结束,并将运行结果返回,如果在主线程需要使用,我们就可以直接使用了。
PS:技术有限,如有不对之处,欢迎大家指出。
相关文章推荐
- Java线程中sleep()、wait()和notify()和notifyAll()、yield()、join()等方法的用法和区别
- java线程当中 sleep() join() yield()方法的简单比较以及如何保护共享数据介绍
- java的join和yield方法处理线程顺序执行
- java Thread 线程的.Join()方法
- 关闭超时的java线程 --利用join和interrupt方法
- java线程中join()方法
- Java线程中yield与join方法的区别
- java线程wait,notify,yield,join方法
- Java多线程 小知识 守护线程(后台线程)setDaemon、join方法、优先级、yield方法
- Java线程中sleep()、wait()和notify()和notifyAll()、yield()、join()等方法的用法和区别
- Java线程中sleep()、wait()和notify()和notifyAll()、yield()、join()等方法的用法和区别
- java基础:线程方法之join
- Java 线程与并发研究系列三(join()方法使用)
- java 多线程(1) join() / interrupt() 打断sleep() / stop() / run() / 线程停止的方法
- Java线程中sleep()、wait()和notify()和notifyAll()、yield()、join()等方法的用法和区别
- Java线程--interrupt join yield setDaemon常用方法的使用
- Java中的线程Thread方法之---join()
- Java中的线程Thread方法之---join()
- Java线程中yield与join方法的区别
- Java线程中yield与join方法的区别