zoukankan      html  css  js  c++  java
  • GUC-8 小练习

    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    /*
     * 编写一个程序,开启 3 个线程,这三个线程的 ID 分别为 A、B、C,每个线程将自己的 ID 在屏幕上打印 10 遍,要求输出的结果必须按顺序显示。
     *    如:ABCABCABC…… 依次递归
     */
    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();
        
        /**
         * @param totalLoop : 循环第几轮
         */
        public void loopA(int totalLoop){
            lock.lock();
            
            try {
                //1. 判断
                if(number != 1){
                    condition1.await();
                }
                
                //2. 打印
                for (int i = 1; i <= 1; i++) {
                    System.out.println(Thread.currentThread().getName() + "	" + i + "	" + totalLoop);
                }
                
                //3. 唤醒
                number = 2;
                condition2.signal();
            } 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 <= 1; i++) {
                    System.out.println(Thread.currentThread().getName() + "	" + i + "	" + totalLoop);
                }
                
                //3. 唤醒
                number = 3;
                condition3.signal();
            } 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 <= 1; i++) {
                    System.out.println(Thread.currentThread().getName() + "	" + i + "	" + totalLoop);
                }
                
                //3. 唤醒
                number = 1;
                condition1.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
        
    }
  • 相关阅读:
    mysql在虚拟机上重启命令
    mysql最佳优化经验
    mysql索引优化面试题
    Java 23种设计模式详尽分析与实例解析之三--行为型模式
    几种经典的数据排序及其Java实现
    os内存使用管理之linux篇
    C++基础学习笔记----第四课(函数的重载、C和C++的相互调用)
    os内存使用管理之unix-AIX篇
    Spring3.0 入门进阶(1):从配置文件装载Bean
    网上销售第二天有感
  • 原文地址:https://www.cnblogs.com/surge/p/10476256.html
Copyright © 2011-2022 走看看