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();
        }
    }
  • 相关阅读:
    underscore相关记录
    背包问题
    数学图形(2.26) 3D曲线结
    数学图形(1.41)super spiral超级螺线
    数学图形(2.25)三维悬链线与悬链面
    数学图形(2.24) 帖在圆柱面上的曲线
    数学图形(2.23)Cylindric sine wave柱面正弦曲线
    数学图形(2.22) 帖在圆锥面上的曲线
    数学图形(2.21) 帖在抛物面上的曲线
    数学图形(2.20)3D曲线
  • 原文地址:https://www.cnblogs.com/catpainter/p/12715331.html
Copyright © 2011-2022 走看看