zoukankan      html  css  js  c++  java
  • 多线程之join方法

    join作用是让其他线程变为等待,我先执行。thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程(先执行指定的线程,再执行当前的线程)。比如在线程B(如主线程)中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B。

    public class Demo11Join {
      public static void main(String[] args) {
        JoinThread joinThread = new JoinThread();
        Thread thread1 = new Thread(joinThread, "线程1");
        Thread thread2 = new Thread(joinThread, "线程2");
        Thread thread3 = new Thread(joinThread, "线程3");
        thread1.start();
        thread2.start();
        thread3.start();
        try {
          thread1.join();
       } catch (Exception e) {
       }
        for (int i = 0; i < 5; i++) {
          System.out.println("main ---i:" + i);
       }
     }
      static class JoinThread implements Runnable {
        private Random random = new Random();
        public void run() {
          String name = Thread.currentThread().getName();
          for (int i = 0; i < 5; i++) {
            try {
              Thread.sleep(random.nextInt(10));
           } catch (InterruptedException e) {
              e.printStackTrace();
           }
            System.out.println(name + "内容是:" + i);
         }
       }
     }
    }

    其中一次执行结果如下:

    线程2内容是:0
    线程1内容是:0
    线程1内容是:1
    线程2内容是:1
    线程3内容是:0
    线程2内容是:2
    线程2内容是:3
    线程3内容是:1
    线程1内容是:2
    线程3内容是:2
    线程2内容是:4
    线程3内容是:3
    线程1内容是:3
    线程1内容是:4
    main ---i:0
    main ---i:1
    main ---i:2
    main ---i:3
    main ---i:4
    线程3内容是:4

    如果不调用线程1的join方法:

    public class Demo11Join {
      public static void main(String[] args) {
        JoinThread joinThread = new JoinThread();
        Thread thread1 = new Thread(joinThread, "线程1");
        Thread thread2 = new Thread(joinThread, "线程2");
        Thread thread3 = new Thread(joinThread, "线程3");
        thread1.start();
        thread2.start();
        thread3.start();
        for (int i = 0; i < 5; i++) {
          System.out.println("main ---i:" + i);
       }
     }
      static class JoinThread implements Runnable {
        private Random random = new Random();
        public void run() {
          String name = Thread.currentThread().getName();
          for (int i = 0; i < 5; i++) {
            try {
              Thread.sleep(random.nextInt(10));
           } catch (InterruptedException e) {
              e.printStackTrace();
           }
            System.out.println(name + "内容是:" + i);
         }
       }
     }
    }

    其中一次执行结果如下:

    main ---i:0
    main ---i:1
    main ---i:2
    main ---i:3
    main ---i:4
    线程3内容是:0
    线程2内容是:0
    线程3内容是:1
    线程2内容是:1
    线程3内容是:2
    线程1内容是:0
    线程2内容是:2
    线程2内容是:3
    线程1内容是:1
    线程2内容是:4
    线程3内容是:3
    线程1内容是:2
    线程3内容是:4
    线程1内容是:3
    线程1内容是:4
  • 相关阅读:
    .net开发微信公众号(2)-验证服务器地址
    [广度遍历和深度遍历]聊聊算法和设计模式
    DDD领域驱动设计之聚合、实体、值对象
    DDD领域驱动设计之运用层代码
    DDD领域驱动设计之领域服务
    DDD领域驱动设计之领域基础设施层
    DDD领域驱动设计实践篇之如何提取模型
    Docker最全教程——从理论到实战(七)
    Docker最全教程——从理论到实战(六)
    Docker最全教程——从理论到实战(五)
  • 原文地址:https://www.cnblogs.com/zwh0910/p/15768973.html
Copyright © 2011-2022 走看看