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);
                }
            }
        }
    }
  • 相关阅读:
    用js遍历生成数独可行数据(未优化版本)
    JS生成tips小工具
    Iframe使用
    二级指针作输入的三种内存模型
    货品的进出库模型
    约瑟夫问题
    vector
    CUDA并行简单加法
    第一个CUDA程序
    在Ubuntu下安装、配置和测试cuda[复制]
  • 原文地址:https://www.cnblogs.com/angdh/p/15569635.html
Copyright © 2011-2022 走看看