• 多线程面试题之【三线程按顺序交替打印ABC的方法】


    建立三个线程,线程名字分别为:A、B、C,要求三个线程分别打印自己的线程名字,但是要求三个线程同时运行,并且实现交替打印,即按照ABCABCABC的顺序打印。打印10轮,打印完毕控制台输出字符串:"打印完毕"

    方法之一:使用Condition等待唤醒机制,代码如下:

    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    /**
     * Created by gan on 2019/5/19 10:54.
     */
    public class ConditionDemo {
    
        private static int num = 1; //打印“谁”标识符
    
        private static Lock lock = new ReentrantLock();
        private static final Condition conditionA = lock.newCondition();
        private static final Condition conditionB = lock.newCondition();
        private static final Condition conditionC = lock.newCondition();
    
        private static CountDownLatch countDownLatch = new CountDownLatch(10); //使用闭锁计数器
    
    
        public static void main(String[] args) throws Exception {
    
            long loop = countDownLatch.getCount(); //初始化打印轮数
    
            new Thread(() -> {
                for (int i = 1; i <= loop; i++) {
                    try {
                        printA();
                    } catch (InterruptedException e) {
                    }
                }
            }, "A").start();
    
            new Thread(() -> {
                for (int i = 1; i <= loop; i++) {
                    try {
                        printB();
                    } catch (InterruptedException e) {
                    }
                }
            }, "B").start();
    
            new Thread(() -> {
                for (int i = 1; i <= loop; i++) {
                    try {
                        printC(i);
                    } catch (InterruptedException e) {
                    }
                }
            }, "C").start();
    
            countDownLatch.await();
    
            System.out.println("打印完毕.........");
        }
    
        //打印‘A’的方法
        public static void printA() throws InterruptedException {
            lock.lock();
            if (num != 1) { //标识符等于1的时候打印A
                conditionA.await();
            }
            System.out.print(Thread.currentThread().getName());
            num = 2;
            conditionB.signal();
            lock.unlock();
        }
    
        //打印‘B’的方法
        public static void printB() throws InterruptedException {
            lock.lock();
            if (num != 2) { //标识符等于2的时候打印B
                conditionB.await();
            }
            System.out.print(Thread.currentThread().getName());
            num = 3;
            conditionC.signal();
            lock.unlock();
        }
    
        //打印‘C’的方法
        public static void printC(int loop) throws InterruptedException {
            lock.lock();
            if (num != 3) { //标识符等于3的时候打印A
                conditionC.await();
            }
            System.out.print(Thread.currentThread().getName());
            System.out.print("[" + loop + "]"); //轮数间隔符号
            num = 1;
            conditionA.signal();
            countDownLatch.countDown();
            lock.unlock();
        }
    
    
    }

    运行结果如下:

  • 相关阅读:
    命令行推送文章到博客园
    链表的新写法
    关于vim无法复制到剪切板的问题
    Ethernet & ARP Protocol
    [从今天开始修炼数据结构]串、KMP模式匹配算法
    [从今天开始修炼数据结构]队列、循环队列、PriorityQueue的原理及实现
    [从今天开始修炼数据结构]栈、斐波那契数列、逆波兰四则运算的实现
    [从今天开始修炼数据结构]线性表及其实现以及实现有Itertor的ArrayList和LinkedList
    IntelliJ IDEA快捷键大全
    [从今天开始修炼数据结构]基本概念
  • 原文地址:https://www.cnblogs.com/ganbo/p/10889758.html
Copyright © 2020-2023  润新知