• 【多线程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();
            }
        }
    }
  • 相关阅读:
    EntityFramework的安装
    利用Xml架构生成实体访问类
    C#生成XSD规范
    利用Vistual Studio自带的xsd.exe工具,根据XML自动生成XSD
    在.net中序列化读写xml方法的总结
    MVP设计模式的实现
    c#万能视频播放器
    libavcodec是一款LGPL自由软件编解码库,用于视频和音频数据的编解码工作
    用C#实现多种方式播放Wav声音
    Using the G711 standard
  • 原文地址:https://www.cnblogs.com/july-sunny/p/14961892.html
Copyright © 2020-2023  润新知