zoukankan      html  css  js  c++  java
  • 线程按序交替

    编写一个程序,开启 3 个线程,这三个线程的 ID 分别为 A、B、C,

    每个线程将自己的 ID 在屏幕上打印 10 遍,要 求输出的结果必须按顺序显示

    TestABCAlternate

    package com.aff.juc;
    
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class TestABCAlternate {
        public static void main(String[] args) {
            AlternateDemo ad = new AlternateDemo();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 1; i <= 20; i++) {
                        ad.loopA(i);
                    }
                }
            },"A线程").start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 1; i <= 20; i++) {
                        ad.loopB(i);
                    }
                }
            },"B线程").start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 1; i <=20; i++) {
                        ad.loopC(i);
                        System.out.println("=============================");
                    }
                }
            },"C线程").start();
    
        }
    }
    
    class AlternateDemo {
        private int number = 1;
        private Lock lock = new ReentrantLock();
        private Condition condition1 = lock.newCondition();
        private Condition condition2 = lock.newCondition();
        private Condition condition3 = lock.newCondition();
    
        // totalLoop :循环几轮
        public void loopA(int totalLoop) {
            lock.lock();
            try {
                // 1. 判断
                if (number != 1) {
                    condition1.await();
                }
                // 2.打印
                for (int i = 1; i <= 2; i++) {
                    System.out.println(Thread.currentThread().getName() + 
    " " + i +" "+ " " + totalLoop); } // 3.唤醒 number = 2; condition2.signal();// number为2 唤醒condition2 } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } public void loopB(int totalLoop) { lock.lock(); try { // 1. 判断 if (number != 2) { condition2.await(); } // 2.打印 for (int i = 1; i <= 2; i++) { System.out.println(Thread.currentThread().getName() +
    " " + " "+ i + " " + totalLoop); } // 3.唤醒 number = 3; condition3.signal();// number为3 唤醒condition3 } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } public void loopC(int totalLoop) { lock.lock(); try { // 1. 判断 if (number != 3) { condition3.await(); } // 2.打印 for (int i = 1; i <= 2; i++) { System.out.println(Thread.currentThread().getName() +
    " " + " "+ i + " " + totalLoop); } // 3.唤醒 number = 1; condition1.signal();// number为1 唤醒condition1 } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } }
    All that work will definitely pay off
  • 相关阅读:
    品鉴-宋词
    【转载】全球水质最棒的十大景点
    Python文档管理与格式化工具
    Python音频处理
    Python剪切板提取、截图、图片粘贴,操作汇总
    Python多进程
    Wifi配置
    条码生成与解析
    谎言: “太空能看到的惟一的人工痕迹,长城!”
    VNC-Server安装配置详解
  • 原文地址:https://www.cnblogs.com/afangfang/p/12632559.html
Copyright © 2011-2022 走看看