zoukankan      html  css  js  c++  java
  • 经典多线程问题(六)-零奇偶问题01020304

    1.0 countdownlatch关键字的使用

    package com.example.demo;
    
    import java.util.concurrent.CountDownLatch;
    
    /**
     * @ClassName ZeroEvenOdd
     * @Description: 1116. 打印零与奇偶数(多线程)
     * @Author xtanb
     * @Date 2019/9/23
     * @Version V1.0
     **/
    public class ZeroEvenOdd {
        private int n;
        private CountDownLatch countDownLatchA;
        private CountDownLatch countDownLatchB;
        private CountDownLatch countDownLatchC;
    
        public ZeroEvenOdd(int n) {
            this.n = n;
            this.countDownLatchA = new CountDownLatch(0);
            this.countDownLatchB = new CountDownLatch(1);
            this.countDownLatchC = new CountDownLatch(1);
        }
    
        // printNumber.accept(x) outputs "x", where x is an integer.
        public void zero(IntConsumer printNumber) throws InterruptedException {
            for(int i=0;i<n;i++){
                countDownLatchA.await();
                printNumber.accept(0);
                countDownLatchA = new CountDownLatch(1);
                if(i%2==0){
                    countDownLatchB.countDown();
                }else{
                    countDownLatchC.countDown();
                }
            }
        }
    
        public void even(IntConsumer printNumber) throws InterruptedException {
            for(int i=1;i<=n;i=i+2){
                countDownLatchB.await();
                printNumber.accept(i);
                countDownLatchB = new CountDownLatch(1);
                countDownLatchA.countDown();
            }
        }
    
        public void odd(IntConsumer printNumber) throws InterruptedException {
            for(int i=2;i<=n;i=i+2){
                countDownLatchC.await();
                printNumber.accept(i);
                countDownLatchC = new CountDownLatch(1);
                countDownLatchA.countDown();
            }
        }
    
        public static void main(String[] args) {
            ZeroEvenOdd zeroEvenOdd = new ZeroEvenOdd(10);
            IntConsumer printNumber = new IntConsumer();
            new Thread(){
                @Override
                public void run() {
                    try {
                        zeroEvenOdd.zero(printNumber);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
            new Thread(){
                @Override
                public void run() {
                    try {
                        zeroEvenOdd.odd(printNumber);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
            new Thread(){
                @Override
                public void run() {
                    try {
                        zeroEvenOdd.even(printNumber);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }
    }
  • 相关阅读:
    C# 读写xml
    oracle经验小节2
    解决在IE9,IE10浏览器下,程序没有任何错误,easy ui页面不加载任何数据的问题
    解决调试不能命中断点的问题
    在做和sap系统集成的一点心得
    easy ui datagrid 数据分页
    selector-item属性
    scaleType-模拟按钮加文字整天点击效果
    layout-代码中添加view
    linearlayout-weight 属性作用
  • 原文地址:https://www.cnblogs.com/helloworldmybokeyuan/p/11718487.html
Copyright © 2011-2022 走看看