zoukankan      html  css  js  c++  java
  • Java多线程(2):线程加入/join()

    线程加入

    join()方法,等待其他线程终止。在当前线程(主线程)中调用另一个线程(子线程)的join()方法,则当前线程转入阻塞状态,直到另一个线程运行结束,当前线程再由阻塞转为就绪状态

    也就是主线程中,子线程调用了join()方法后面的代码,只有等到子线程结束了才能执行。

    在很多情况下,主线程生成并起动了子线程,如果子线程里要进行大量的耗时的运算,主线程往往将于子线程之前结束。

    如下例子:

     1 class JoinRunnable implements Runnable {
     2     @Override
     3     public void run() {
     4         System.out.println("Child thread start.");
     5         try {
     6             for (int i = 1; i <= 5; i++) {
     7                 System.out.println("Child thread runing [" + i + "].");
     8                 Thread.sleep((int) (Math.random() * 100));
     9             }
    10         } catch (InterruptedException e) {
    11             System.out.println("Child thread interrupted.");
    12         }
    13         System.out.println("Child thread end.");
    14     }
    15 }
    16 
    17 public class JoinDemo {
    18     // main是主线程
    19     public static void main(String args[]) {
    20         System.out.println("Main thread start.");
    21         new Thread(new JoinRunnable()).start();
    22         System.out.println("Main thread end.");
    23     }
    24 }

    执行结果:

    Main thread start.
    Main thread end.
    Child thread start.
    Child thread runing [1].
    Child thread runing [2].
    Child thread runing [3].
    Child thread runing [4].
    Child thread runing [5].
    Child thread end.

    如果主线程需要等待子线程执行完成之后再结束,这个时候就要用到join()方法了。

    如下例子:

     1 class JoinRunnable implements Runnable {
     2     @Override
     3     public void run() {
     4         System.out.println("Child thread start.");
     5         try {
     6             for (int i = 1; i <= 5; i++) {
     7                 System.out.println("Child thread runing [" + i + "].");
     8                 Thread.sleep((int) (Math.random() * 100));
     9             }
    10         } catch (InterruptedException e) {
    11             System.out.println("Child thread interrupted.");
    12         }
    13         System.out.println("Child thread end.");
    14     }
    15 }
    16 
    17 public class JoinDemo {
    18     // main是主线程
    19     public static void main(String args[]) {
    20         System.out.println("Main thread start.");
    21         Thread t = new Thread(new JoinRunnable());
    22         t.start();
    23         try {
    24             t.join();
    25         } catch (InterruptedException e) {
    26             e.printStackTrace();
    27         }
    28         System.out.println("Main thread end.");
    29     }
    30 }

    执行结果:

    Main thread start.
    Child thread start.
    Child thread runing [1].
    Child thread runing [2].
    Child thread runing [3].
    Child thread runing [4].
    Child thread runing [5].
    Child thread end.
    Main thread end.
  • 相关阅读:
    内置对象,监听器,过滤器
    20169201 使用Metaspoit攻击MS08-067实验
    20169201 2016-2017-2《网络攻防》课程总结
    20169201 2016-2017-2 《网络攻防》第十四周作业
    20169201 2016-2017-2 《网络攻防实践》第11周学习总结
    20169201 2016-2017-2 《移动平台开发实践》第10周学习总结
    20169201 2016-2017-2 《网络攻防实践》第10周学习总结
    20169201 实验三 敏捷开发与XP实践报告
    20169201 2016-2017-2 《网络攻防实践》 实验二
    20169201 2016-2017-2 实验一 网络攻防实验环境的搭建与测试
  • 原文地址:https://www.cnblogs.com/storml/p/7611896.html
Copyright © 2011-2022 走看看