• join()介绍及三个线程顺序输出实现


    一、join介绍

      Thead.join()函数在使用后,会挂起调用线程,直到被调用线程结束执行,调用线程才会继续执行。源码中调用join后,方法会一直检测要join()的线程是否存活(isAlive()方法),直到线程执行完成后,调用线程的this.notifyAll()方法,才会回到刚刚挂起的主程序。基本操作如下:

    public class Worker extends Thread {
    
        // 工作者名
        private String name;
        // 工作时间
        private long time;
    
        public Worker(String name, long time) {
            this.name = name;
            this.time = time;
        }
    
        @Override
        public void run() {
            // TODO 自动生成的方法存根
            try {        
                System.out.println(name + "开始工作");
         Thread.sleep(time);   
                System.out.println(name + "工作完成,耗费时间=" + time);
            } catch (InterruptedException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
    
    }
    
    public static void main(String[] args) throws InterruptedException {
            // TODO Auto-generated method stub
            Worker worker0 = new Worker("worker0", (long) (Math.random() * 1000));
            Worker worker1 = new Worker("worker1", (long) (Math.random() * 1000 + 1000));
            Worker worker2 = new Worker("worker2", (long) (Math.random() * 1000 + 2000));
            
            worker1.start();
            worker0.start();
    
            worker1.join();
            worker0.join();
            System.out.println("准备工作就绪");
    
            worker2.start();
        }

      结果如下:

    二、join方式代码实现

      有了上面的介绍很容易想到,三个线程顺序输出,让他们依次join(),按顺序等待执行就好了,代码如下:

    public static void main(String[] args) {
            final Thread t1 = new Thread(new Runnable() {
                public void run() {
                    System.out.println(Thread.currentThread().getName() + " run 1");
                }
            }, "T1");
            final Thread t2 = new Thread(new Runnable() {
                public void run() {
    
                    try {
                        t1.join();
                        System.out.println(Thread.currentThread().getName() + " run 2");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "T2");
            final Thread t3 = new Thread(new Runnable() {
                public void run() {
                    try {
                        t2.join();
                        System.out.println(Thread.currentThread().getName() + " run 3");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "T3");
    
            t1.start();
            t2.start();
            t3.start();
            
            //方法二
            // ExecutorService executor = Executors.newSingleThreadExecutor();
            // executor.submit(t1);
            // executor.submit(t2);
            // executor.submit(t3);
            // executor.shutdown();
        }

      效果如图:

     

    三、既然是多线程,自然也可以用加锁的方式实现,直接上代码:

    package com.concurrency;
    
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class ThreeTheadPrintByOrder2_Lock {
    
        public static void main(String[] args) {
            new ThreadA().start();
            new ThreadB().start();
            new ThreadC().start();
        }
    
        private static Lock lock = new ReentrantLock();
        private static int state = 0;
    
        static class ThreadA extends Thread {
            @Override
            public void run() {
                for (int i = 0; i < 10;) {
                    lock.lock();
                    if (state % 3 == 0) {
                        System.out.println(Thread.currentThread().getName() + " A");
                        state++;
                        i++;
                    }
                    lock.unlock();
                }
            }
        }
    
        static class ThreadB extends Thread {
            @Override
            public void run() {
                for (int i = 0; i < 10;) {
                    lock.lock();
                    if (state % 3 == 1) {
                        System.out.println(Thread.currentThread().getName() + " B");
                        state++;
                        i++;
                    }
                    lock.unlock();
                }
            }
        }
    
        static class ThreadC extends Thread {
            @Override
            public void run() {
                for (int i = 0; i < 10;) {
                    lock.lock();
                    if (state % 3 == 2) {
                        System.out.println(Thread.currentThread().getName() + " C");
                        state++;
                        i++;
                    }
                    lock.unlock();
                }
            }
        }
    
    }

      效果如下:

      

  • 相关阅读:
    前端代码规范
    使用CSS设置行间距,字间距.
    今天我们要说的画一个三角形,恩,画一个三角形,第一种呢是利用我们的html标签结合css来实现;而第二种方法就就是我们的html5新增的一个标签canves,这个canves就是网页画幕,那么顾名思义就是在网页里建造一个画板,用来画画,好,那接下来就和我一起去看看吧!
    javascript基础程序(算出一个数的平方值、算出一个数的阶乘、输出!- !- !- !- !- -! -! -! -! -! 、函数三个数中的最大数)
    css3 风车旋转
    css3简单的图片轮播
    css3 2D动画的基本用法和介绍
    Android 系统UI设计规则
    Android用户界面程序设计示例
    将博客搬至CSDN
  • 原文地址:https://www.cnblogs.com/lyInfo/p/9130094.html
Copyright © 2020-2023  润新知