• JAVA JUC 线程按顺序执行


    class demo{
        private ReentrantLock lock =new ReentrantLock();
        private Condition cb = lock.newCondition();
        private Condition cc = lock.newCondition();
        private Condition ca = lock.newCondition();
        private int num = 1;
        public void loopA(int epoch){
            lock.lock();
    
            try {
                if(num!=1){
    
                    ca.await();
    
                }
                for(int i=0;i<5;i++){
                    System.out.println(Thread.currentThread().getName()+"  "+i+"	"+epoch);
                }
                num=2;
                cb.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
    
        }
        public void loopB(int epoch){
            lock.lock();
    
            try {
                if(num!=2){
                    cb.await();
                }
                for(int i=0;i<5;i++){
                    System.out.println(Thread.currentThread().getName()+"  "+i+"	"+epoch);
                }
                num=3;
                cc.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
    
        }
        public void loopC(int epoch){
            lock.lock();
    
            try {
                if(num!=3){
                   cc.await();
                }
                for(int i=0;i<5;i++){
                    System.out.println(Thread.currentThread().getName()+"  "+i+"	"+epoch);
                }
                num=1;
                ca.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
    
        }
    }
    public class alterDemo {
        public static void main(String[] args) {
            demo d = new demo();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for(int i=0;i<20;i++)
                    {
                        d.loopA(i);
                    }
    
                }
            },"A").start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for(int i=0;i<20;i++)
                    {
                        d.loopB(i);
                    }
    
                }
            },"B").start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for(int i=0;i<20;i++)
                    {
                        d.loopC(i);
                    }
    
                }
            },"C").start();
        }
    }
    View Code
  • 相关阅读:
    用DECODE进行排序
    linux下批量替换文件内容
    Linux下chkconfig命令详解
    linux 命令参数列表过长以及find用法
    参数上使用自定义注解在aop中无法获取到该参数
    AOP
    AOP aspect XML 配置
    AOP前世与今生,aspect
    ETL工具之——kettle使用简介
    ETL工具之kittle使用案例整理
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/12470290.html
Copyright © 2020-2023  润新知