zoukankan      html  css  js  c++  java
  • 三个线程按顺序循环输出ABC ABC ABC

    一、Synchronized解决

    import java.util.concurrent.*;
    
    public class Test {
    
        public static void main(String[] args) throws Exception {
            ExecutorService pool = Executors.newFixedThreadPool(3);
            PrintTask task = new PrintTask();
            pool.execute(() -> {
                try {
                    task.printA();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            pool.execute(() -> {
                try {
                    task.printB();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            pool.execute(() -> {
                try {
                    task.printC();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            pool.shutdown();
        }
    
    
        private static class PrintTask{
            private volatile static Integer flag = 0;
    
            private synchronized void printA() throws InterruptedException {
                while (true){
                    if (flag == 0){
                        System.out.println(Thread.currentThread().getName() + "-----A");
                        Thread.sleep(500);
                        flag = 1;
                        notifyAll();
                    }else{
                        wait();
                    }
                }
            }
    
            private synchronized void printB() throws InterruptedException {
                while (true){
                    if (flag == 1){
                        System.out.println(Thread.currentThread().getName() + "-----B");
                        Thread.sleep(500);
                        flag = 2;
                        notifyAll();
                    }else{
                        wait();
                    }
                }
            }
    
            private synchronized void printC() throws InterruptedException {
                while (true){
                    if (flag == 2){
                        System.out.println(Thread.currentThread().getName() + "-----C");
                        Thread.sleep(500);
                        flag = 0;
                        notifyAll();
                    }else{
                        wait();
                    }
                }
            }
        }
    
    }

    输出:

    二、lock解决

    import java.util.concurrent.*;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class Test {
    
        public static void main(String[] args) throws Exception {
            ExecutorService pool = Executors.newFixedThreadPool(3);
            PrintTask task = new PrintTask();
            pool.execute(() -> {
                try {
                    task.printA();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            pool.execute(() -> {
                try {
                    task.printB();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            pool.execute(() -> {
                try {
                    task.printC();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            pool.shutdown();
        }
    
    
        private static class PrintTask{
            private volatile static Integer flag = 0;
            private Lock lock = new ReentrantLock();
            private Condition conditionA = lock.newCondition();
            private Condition conditionB = lock.newCondition();
            private Condition conditionC = lock.newCondition();
    
            private void printA() throws InterruptedException {
                try {
                    lock.lock();
                    while (true) {
                        if (flag == 0) {
                            System.out.println(Thread.currentThread().getName() + "-----A");
                            Thread.sleep(500);
                            flag = 1;
                            conditionB.signal();
                        } else {
                            conditionA.await();
                        }
                    }
                } finally {
                    lock.unlock();
                }
            }
    
            private void printB() throws InterruptedException {
                try {
                    lock.lock();
                    while (true) {
                        if (flag == 1) {
                            System.out.println(Thread.currentThread().getName() + "-----B");
                            Thread.sleep(500);
                            flag = 2;
                            conditionC.signal();
                        } else {
                            conditionB.await();
                        }
                    }
                } finally {
                    lock.unlock();
                }
            }
    
            private void printC() throws InterruptedException {
                try {
                    lock.lock();
                    while (true) {
                        if (flag == 2) {
                            System.out.println(Thread.currentThread().getName() + "-----C");
                            Thread.sleep(500);
                            flag = 0;
                            conditionA.signal();
                        } else {
                            conditionC.await();
                        }
                    }
                } finally {
                    lock.unlock();
                }
            }
        }
    
    }

    输出:

    三、信号量解决

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Semaphore;
    
    public class Test {
    
        public static void main(String[] args) throws Exception {
            ExecutorService pool = Executors.newFixedThreadPool(3);
            PrintTask task = new PrintTask();
            pool.execute(() -> {
                try {
                    task.printA();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            pool.execute(() -> {
                try {
                    task.printB();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            pool.execute(() -> {
                try {
                    task.printC();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            pool.shutdown();
        }
    
    
        private static class PrintTask{
            private Semaphore semaphoreA = new Semaphore(1);
            private Semaphore semaphoreB = new Semaphore(0);
            private Semaphore semaphoreC = new Semaphore(0);
    
            private void printA() throws InterruptedException {
                while (true) {
                    semaphoreA.acquire();
                    System.out.println(Thread.currentThread().getName() + "-----A");
                    Thread.sleep(500);
                    semaphoreB.release();
                }
            }
    
            private void printB() throws InterruptedException {
                while (true) {
                    semaphoreB.acquire();
                    System.out.println(Thread.currentThread().getName() + "-----B");
                    Thread.sleep(500);
                    semaphoreC.release();
                }
            }
    
            private void printC() throws InterruptedException {
                while (true) {
                    semaphoreC.acquire();
                    System.out.println(Thread.currentThread().getName() + "-----C");
                    Thread.sleep(500);
                    semaphoreA.release();
                }
            }
        }
    
    }

    注意:多次调用release,或release(int),可以动态增加permits的个数,构造参数中的permits数量是初始值,不是最终的许可数量。

    输出:

  • 相关阅读:
    HDU1255 覆盖的面积 —— 求矩形交面积 线段树 + 扫描线 + 离散化
    HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化
    HDU1540 Tunnel Warfare —— 线段树 区间合并
    HDU3974 Assign the task —— dfs时间戳 + 线段树
    HDU4027 Can you answer these queries? —— 线段树 区间修改
    POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值
    ZOJ1610 Count the Colors —— 线段树 区间染色
    HDU1698 Just a Hook —— 线段树 区间染色
    POJ2528 Mayor's posters —— 线段树染色 + 离散化
    POJ3468 A Simple Problem with Integers —— 线段树 区间修改
  • 原文地址:https://www.cnblogs.com/LUA123/p/13033218.html
Copyright © 2011-2022 走看看