zoukankan      html  css  js  c++  java
  • 三个线程循环打印ABC问题

    问题描述:三个线程,第一个线程只能打印A,第二个线程只能打印B,第三个线程只能打印C,如何循环打印出一定次数的ABC?

    例如:打印循环3次,输出为ABCABCABC

    lock方式:

    public class PrintABC {
        private Integer times;
    
        private Integer flag;
    
        private Lock lock;
    
        public PrintABC(Integer times, ReentrantLock lock) {
            this.flag = 0;
            this.times = times;
            this.lock = lock;
        }
    
        public void printA() {
            print("A", 0);
        }
    
        public void printB() {
            print("B", 1);
        }
    
        public void printC() {
            print("C", 2);
        }
    
        public void print(String s, int value) {
            for (int i = 0; i < times;) {
                lock.lock();
                if (flag == value) {
                    System.out.printf("%s", s);
                    flag = (flag + 1) % 3;
                    i++;
                }
                lock.unlock();
            }
        }
    
        public static void main(String[] args) {
            PrintABC printABC = new PrintABC(new Integer(10), new ReentrantLock());
            new Thread(printABC::printA).start();
            new Thread(printABC::printB).start();
            new Thread(printABC::printC).start();
        }
    }

    lock + condition方式:

    public class PrintABC2 {
        private Lock lock;
    
        private int flag;
    
        private int times;
    
        Condition[] condition = new Condition[3];
    
        public PrintABC2(int times, Lock lock) {
            this.lock = lock;
            this.times = times;
            this.flag = 0;
            for (int i = 0; i < 3; i++) {
                this.condition[i] = lock.newCondition();
            }
        }
    
        public void printA() {
            print("A", 0);
        }
    
        public void printB() {
            print("B", 1);
        }
    
        public void printC() {
            print("C", 2);
        }
    
        public void print(String s, int value) {
            try {
                for (int i = 0; i < times; i++) {
                    lock.lock();
                    if (flag != value) {
                        condition[value].await();
                    }
                    System.out.printf("%s", s);
                    flag = (flag + 1) % 3;
                    condition[flag].signal();
                    lock.unlock();
                }
            } catch (InterruptedException e) {
                System.out.println(e);
            }
        }
        public static void main(String[] args) {
            PrintABC2 printABC = new PrintABC2(new Integer(10), new ReentrantLock());
            new Thread(printABC::printA).start();
            new Thread(printABC::printB).start();
            new Thread(printABC::printC).start();
        }
    }

    信号量方式:

    public class PrintABC3 {
        private int times;
    
        private Semaphore[] semaphores = new Semaphore[] {new Semaphore(1), new Semaphore(0), new Semaphore(0)};
    
        public PrintABC3(int times) {
            this.times = times;
        }
    
        public void printA() {
            print("A", 0);
        }
    
        public void printB() {
            print("B", 1);
        }
    
        public void printC() {
            print("C", 2);
        }
    
        public void print(String s, int currendId) {
            int nextId = (currendId + 1) % 3;
            try {
                for (int i = 0; i < times; i++) {
                    semaphores[currendId].acquire();
                    System.out.printf("%s", s);
                    semaphores[nextId].release();
                }
            } catch (InterruptedException e) {
                System.out.println(e);
            }
            return;
        }
    
        public static void main(String[] args) {
            PrintABC3 printABC = new PrintABC3(10);
            new Thread(printABC::printA).start();
            new Thread(printABC::printB).start();
            new Thread(printABC::printC).start();
        }
    }
  • 相关阅读:
    我爬取了爬虫岗位薪资,分析后发现爬虫真香
    红薯,撑起父亲的快乐,让我揪心
    跨域问题服务端解决办法 Request header field Authorization is not allowed by Access-Control-Allow-Headers
    antdvue2.x 使用阿里iconfont自定义组件iconfont
    前端 crypto-js aes 加解密
    jsencrypt加密解密字符串
    CryptoJS base64使用方法
    客户端js生成rsa 密钥对
    js动态添加style样式
    PHP 使用非对称加密算法(RSA)
  • 原文地址:https://www.cnblogs.com/catpainter/p/12715331.html
Copyright © 2011-2022 走看看