zoukankan      html  css  js  c++  java
  • 多线程输出123以及有序输出1-75

    一、多线程实现输出循环123

      1、循环输出123,通过Lock锁和共享变量state实现3个线程循环执行

    private static volatile int state;
        final static Lock lock = new ReentrantLock();
    
        public static void main(String[] args) {
            for (int i = 0; i < 3; i ++) {
    
            }
            Worker a = new Worker(3, 0);
            Worker b = new Worker(3, 1);
            Worker c = new Worker(3, 2);
            a.start();
            b.start();
            c.start();
        }
    
        private static class Worker extends Thread {
    
            int mod;
            int v;
    
            Worker(int mod, int v) {
                this.mod = mod;
                this.v = v;
            }
    
            @Override
            public void run() {
                while (true) {
                    lock.lock();
                    if (state % mod == v) {
                        System.out.print(v + 1);
                        state++;
                    }
                    lock.unlock();
                }
            }
        }

    二、按照顺序输出1-75

        2、通过condition条件的通知机制和原子变量Inc自增实现输出1-75

        

    private static final AtomicInteger inc = new AtomicInteger();
    
        private static Lock lock = new ReentrantLock();
    
    
    
        public static void main(String[] args) {
    
            Condition conditionA = lock.newCondition();
    
            Condition conditionB = lock.newCondition();
    
            Condition conditionC = lock.newCondition();
    
            Worker workerA = new Worker(conditionA, conditionB, "A");
            Worker workerB = new Worker(conditionB, conditionC, "B");
            Worker workerC = new Worker(conditionC, conditionA, "C");
    
            workerA.start();
            workerB.start();
            workerC.start();
    
        }
    
    
    
        private static class Worker extends Thread {
            private Condition condition;
    
            private Condition nextCondition;
    
            private String name;
    
            Worker(Condition condition, Condition nextCondition, String name) {
                this.condition = condition;
                this.nextCondition = nextCondition;
                this.name = name;
            }
    
            @Override
            public void run() {
                lock.lock();
                for (int i = 0; i < 5; i++) {
                    for (int j = 0; j < 5; j ++) {
                        System.out.println("name: " + name + " v: " + inc.incrementAndGet());
                    }
                    nextCondition.signalAll();
    
                    if (i < 5) {
                        try {
                            condition.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                lock.unlock();
            }
        }
  • 相关阅读:
    bzoj1562: [NOI2009]变换序列
    bzoj2763: [JLOI2011]飞行路线
    有上下界网络流
    bzoj3211: 花神游历各国
    bzoj3668: [Noi2014]起床困难综合症
    bzoj2743: [HEOI2012]采花
    BZOJ 1787: [Ahoi2008]Meet 紧急集合
    BZOJ 1029 [JSOI2007]建筑抢修 贪心
    BZOJ 2748 音量调节
    BZOJ 3524: [Poi2014]Couriers 主席树
  • 原文地址:https://www.cnblogs.com/junxing/p/11033646.html
Copyright © 2011-2022 走看看