zoukankan      html  css  js  c++  java
  • 【多线程0】三个线程循环打印ABC

    package com.leigod.user.middle.service.impl;
    
    import lombok.SneakyThrows;
    
    /**
     * @author Sam.yang
     * @since 2021/3/29 17:58
     */
    public class Test {
    
        //定义锁对象
        Object lock = new Object();
    
        //定义通知对象
        boolean tagA = true;
        boolean tagB = false;
        boolean tagC = false;
    
    
        public static void main(String[] args) {
    
            Test test = new Test();
            for (int i = 0; i < 100; i++) {
                Thread threadA = new Thread(new Runnable() {
                    @SneakyThrows
                    @Override
                    public void run() {
                        test.printA(Thread.currentThread().getName());
                    }
                });
    
                Thread threadB = new Thread(new Runnable() {
                    @SneakyThrows
                    @Override
                    public void run() {
                        test.printB(Thread.currentThread().getName());
                    }
                });
    
                Thread threadC = new Thread(new Runnable() {
                    @SneakyThrows
                    @Override
                    public void run() {
                        test.printC(Thread.currentThread().getName());
                    }
                });
    
                threadA.run();
                threadB.run();
                threadC.run();
            }
    
    
    
        }
    
        /**
         * 打印A
         */
        private void printA(String tName) throws InterruptedException {
            synchronized (lock) {
                while (!tagA) {
                    lock.wait();
                }
                System.out.println("A");
                tagA = false;
                tagB = true;
                lock.notify();
            }
        }
    
        /**
         * 打印B
         */
        private void printB(String tName) throws InterruptedException {
            synchronized (lock) {
                while (!tagB) {
                    lock.wait();
                }
                System.out.println("B");
                tagA = false;
                tagB = false;
                tagC = true;
                lock.notify();
            }
        }
    
        /**
         * 打印C
         */
        private void printC(String tName) throws InterruptedException {
            synchronized (lock) {
                while (!tagC) {
                    lock.wait();
                }
                System.out.println("C");
                tagA = true;
                tagB = false;
                tagC = false;
                lock.notify();
            }
        }
    }
  • 相关阅读:
    Android 请求运行时权限
    Android 调试技巧之快速重启生效
    ubuntu 用aptitude代替apt-get处理依赖性问题
    Learning Git by Animations
    Android调试技巧
    Android O PackageInstaller 解析
    阅读代码工具:Visual Studio Code
    Android 缓存策略demo
    |= 和 &= ~
    Android 逆向工具
  • 原文地址:https://www.cnblogs.com/july-sunny/p/14961892.html
Copyright © 2011-2022 走看看