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();
            }
        }
    }
  • 相关阅读:
    sqlserver获取当前id的前一条数据和后一条数据
    C#实现测量程序运行时间及cpu使用时间
    类库dll引用不成功问题
    合并相同字段
    Android之来历
    XML and JSON 验证
    特殊符号
    git 使用
    格式化字符串:金额
    grunt + sass 使用记录
  • 原文地址:https://www.cnblogs.com/july-sunny/p/14961892.html
Copyright © 2011-2022 走看看