美文网首页
三个线程交互打印

三个线程交互打印

作者: xiaohei_e853 | 来源:发表于2022-03-02 11:28 被阅读0次
    package test;
    
    import java.util.concurrent.atomic.AtomicInteger;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class SequencePrintNumber {
    
    
    
    
        public static void main(String[] args) {
    
            ReentrantLock reentrantLock = new ReentrantLock();
            Condition condition1 = reentrantLock.newCondition();
            Condition condition2 = reentrantLock.newCondition();
            Condition condition3 = reentrantLock.newCondition();
    
            AtomicInteger atomicInteger = new AtomicInteger();
    
            new Thread("A"){
                @Override
                public void run() {
                    super.run();
    
                    for (int i = 0; i < 10; i++) {
    
                        try{
                            reentrantLock.lock();
    
                            int count = atomicInteger.get();
    
                            if(count%3!=0){
    
                                condition1.await();
                            }
                            System.out.println(Thread.currentThread().getName()+"111");
                            atomicInteger.getAndAdd(1);
    
                            condition2.signal();
    
                        }catch (Exception e){
    
                        }finally {
                            reentrantLock.unlock();
                        }
    
                    }
    
    
                }
            }.start();
    
            new Thread("B"){
                @Override
                public void run() {
                    super.run();
    
                    for (int i = 0; i < 10; i++) {
    
                        try{
                            reentrantLock.lock();
    
                            int count = atomicInteger.get();
    
                            if(count%3!=1){
                                condition2.await();
    
                            }
                            System.out.println(Thread.currentThread().getName()+"222");
                            atomicInteger.getAndAdd(1);
    
                            condition3.signal();
    
                        }catch (Exception e){
    
                        }finally {
                            reentrantLock.unlock();
                        }
    
                    }
    
    
                }
            }.start();
    
            new Thread("C"){
                @Override
                public void run() {
                    super.run();
    
                    for (int i = 0; i < 10; i++) {
    
                        try{
                            reentrantLock.lock();
    
                            int count = atomicInteger.get();
    
                            if(count%3!=2){
                                condition3.await();
    
                            }
                            System.out.println(Thread.currentThread().getName()+"333");
                            atomicInteger.getAndAdd(1);
    
                            condition1.signal();
    
                        }catch (Exception e){
    
                        }finally {
                            reentrantLock.unlock();
                        }
    
                    }
    
    
                }
            }.start();
        }
    }
    
    

    相关文章

      网友评论

          本文标题:三个线程交互打印

          本文链接:https://www.haomeiwen.com/subject/mlvsrrtx.html