• 多线程循环打印abc


    
    import java.util.concurrent.CountDownLatch;
    
    public class PrintXYZ {
        private CountDownLatch xCount = new CountDownLatch(1);
        private CountDownLatch yCount = new CountDownLatch(1);
        private CountDownLatch zCount = new CountDownLatch(1);
    
        public void print() {
    
            Thread threadA = new Thread() {
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        try {
                            xCount.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("X");
                        yCount.countDown(); //忘了具体方法了 应该不对
                        xCount = new CountDownLatch(1);
                    }
                }
            };
    
            Thread threadB = new Thread() {
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        try {
                            yCount.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("Y");
                        zCount.countDown(); //忘了具体方法了 应该不对
                        yCount = new CountDownLatch(1);
                    }
                }
            };
    
            Thread threadC = new Thread() {
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        try {
                            zCount.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("Z");
                        if (i < 9) {
                            xCount.countDown(); //忘了具体方法了 应该不对
                            zCount = new CountDownLatch(1);
                        }
    
                    }
                }
            };
            threadC.start();
            threadB.start();
            threadA.start();
        }
    
        public static void main(String[] args) {
            PrintXYZ printXYZ = new PrintXYZ();
            printXYZ.print();
            printXYZ.xCount.countDown();
        }
    }
    
  • 相关阅读:
    手机如何当电脑的摄像头使用
    内网穿透软件
    如何在laravel框架中使用阿里云的oss
    css position 定位详解
    laravel 速查表
    window10如何查看连接过的wifi密码
    sweetalert弹出层组件
    phpstudy安装 与phpstudy_Windows web面板安装
    程序员修炼之道读后感
    JAVA-WEB-简单的四则运算
  • 原文地址:https://www.cnblogs.com/daichangya/p/14238264.html
Copyright © 2020-2023  润新知