zoukankan      html  css  js  c++  java
  • 多线程打印:三个线程轮流打印0到100

    package club.interview.thread.print;
    
    /**
     * 3个线程从0到100轮流打印。要求输出结果如下
     * Thread-0:0
     * Thread-1:1
     * Thread-2:2
     * Thread-0:3
     * <p>
     * 知识点扫盲:
     * 1. 类锁的wait、notify
     * 2. lambda内部使用局部变量会隐式加final,可以使用成员变量(为了避免外部线程修改了并回收了局部变量,内部仍要访问的问题。实现上是一个副本)
     * <p>
     *
     * @author QuCheng on 2020/4/10.
     */
    public class Zero2Handred {
    
        int n = 0;
    
        /**
         * 常规思路:加锁 -> 不满足条件wait -> 满足条件执行并notifyAll
         */
        private Runnable sync1() {
            return () -> {
                int nameI = Integer.parseInt(Thread.currentThread().getName());
                while (n <= 100) {
                    synchronized (Zero2Handred.class) {
                        if (n % 3 == nameI && n <= 100) {
                            System.out.println("Thread-" + nameI + ":" + n++);
                            Zero2Handred.class.notifyAll();
                        } else {
                            try {
                                Zero2Handred.class.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            };
        }
    
        /**
         * 省掉不必要的wait(),notifyAll()
         */
        private Runnable sync2() {
            return () -> {
                int nameI = Integer.parseInt(Thread.currentThread().getName());
                while (n <= 100) {
                    synchronized (Zero2Handred.class) {
                        if (n % 3 == nameI) {
                            System.out.println("Thread-" + nameI + ":" + n++);
                        }
                    }
                }
            };
        }
    
        /**
         * 不显示加锁 - 问题所在?此处难道是锁粗化,那为什么把n++放在打印里面又出问题
         */
        public Runnable sync3() {
            return () -> {
                int nameI = Integer.parseInt(Thread.currentThread().getName());
                while (n <= 1000) {
                    if (n % 3 == nameI) {
                        System.out.println("Thread-" + nameI + ":" + n);
                        n++;
                    }
                }
            };
        }
    
    
        public static void main(String[] args) {
    
            Zero2Handred z = new Zero2Handred();
    //        Runnable r = z.sync1();
    //        Runnable r = z.sync2);
            Runnable r = z.sync3();
    
            new Thread(r, "0").start();
            new Thread(r, "1").start();
            new Thread(r, "2").start();
        }
    }
    

      

  • 相关阅读:
    图灵科普系列丛书封面有奖征集(贴图送书)
    图灵2010.03书讯
    博客园图灵杯第4届博问大赛(2.27~3.27)
    asp.net运行原理
    IIS与NET桥梁
    offsetParent解释
    XML 操作类库(开源项目)
    W3C不兼容问题(最根本的原因,及解决方案)
    深入理解JavaScript系列
    HttpApplication对象创建的细节
  • 原文地址:https://www.cnblogs.com/nightOfStreet/p/13039664.html
Copyright © 2011-2022 走看看