美文网首页Java
java 并发 join()

java 并发 join()

作者: 静享时光 | 来源:发表于2020-05-09 00:22 被阅读0次

    首先,我们先看下源码中join方法的解释:

    /**
     * Waits for this thread to die. 
     *
     * <p> An invocation of this method behaves in exactly the same
     * way as the invocation
     *
     * <blockquote>
     * {@linkplain #join(long) join}{@code (0)}
     * </blockquote>
     *
     * @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 void join() throws InterruptedException {
        join(0);
    }
    

    翻译其中一个句子:
    Waits for this thread to die. 等待此线程死亡。
    也就是要等待该线程结束,其他线程才能执行。这就是join的作用。
    我们先来看个示例:

    class MyThread implements Runnable {
    
        private int count = 7;
    
        @Override
        public void run() {
            while (count-- > 0) {
                PrintUtils.print(" MyThread ");
            }
        }
    }
    
    class MyThread2 implements Runnable {
        private int count2 = 5;
    
        @Override
        public void run() {
            while (count2-- > 0) {
                try {
                    Thread.sleep(1000);
                    PrintUtils.print(" MyThread2 ");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    public class ThreadTest6 {
    
        public static void main(String[] args) {
            Thread thread = new Thread(new MyThread());
            Thread thread2 = new Thread(new MyThread2());
            thread.start();
            thread2.start();
            try {
                thread2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
                PrintUtils.print("执行join 方法异常");
            }
            PrintUtils.print("main 方法执行完毕");
        }
    
        /**
         *  MyThread
         *  MyThread
         *  MyThread
         *  MyThread
         *  MyThread
         *  MyThread
         *  MyThread
         *  MyThread2
         *  MyThread2
         *  MyThread2
         *  MyThread2
         *  MyThread2
         * main 方法执行完毕
         *
         * Process finished with exit code 0
         */
    }
    

    示例中,在main方法中,MyThread2调用了join方法,由输出结果可以看到,一直等到MyThread2执行完毕,才执行main方法中的打印输出。

    我们再看看另外一个例子,在其他线程中触发某一个线程调用join()方法

    class MyThread3 extends Thread {
    
        private int count = 10;
    
        private MyThread4 myThread4;
    
        public MyThread3(MyThread4 myThread4) {
            this.myThread4 = myThread4;
        }
    
        @Override
        public void run() {
            try {
                myThread4.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            while (count-- > 0) {
                PrintUtils.print(" MyThread3 ");
            }
        }
    }
    
    class MyThread4 extends Thread {
        private int count2 = 20;
    
        @Override
        public void run() {
    //        while (count2-- > 0) {
    //            PrintUtils.print(" MyThread2 ");
    //        }
    
            try {
                Thread.sleep(2000);
                PrintUtils.print(" MyThread4 ");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    
    public class ThreadTest7 {
    
        public static void main(String[] args) {
            MyThread4 thread4 = new MyThread4();
            MyThread3 thread3 = new MyThread3(thread4);
            thread3.start();
            thread4.start();
    //        try {
    //            thread4.join();
    //        } catch (InterruptedException e) {
    //            e.printStackTrace();
    //            PrintUtils.print("执行join 方法异常");
    //        }
            PrintUtils.print("main 方法执行完毕");
        }
        /**
         * main 方法执行完毕
         *  MyThread4
         *  MyThread3
         *  MyThread3
         *  MyThread3
         *  MyThread3
         *  MyThread3
         *  MyThread3
         *  MyThread3
         *  MyThread3
         *  MyThread3
         *  MyThread3 
         */
    }
    

    在MyThread3中,MyThread4的对象调用join方法,在输出的结果中可以看到MyThread3等到MyThrad4执行结束之后才执行。

    总结:
    由上面两个示例可以看出,在A线程中触发了B线程调用join方法,A线程会等待B线程执行完毕之后再执行。
    java编程思想中解释的是:
    如果某个线程在另一个线程t上调用t.join(),此线程将被挂起,直到目标线程t结束才恢复(即t.isAlive()返回为假)。

    相关文章

      网友评论

        本文标题:java 并发 join()

        本文链接:https://www.haomeiwen.com/subject/fxwcnhtx.html