@@ -0,0 +1,2924 @@
+package main.algorithm.leetcode;%0A%0Aimport org.junit.Test;%0A%0Aimport java.util.concurrent.ArrayBlockingQueue;%0Aimport java.util.concurrent.ThreadPoolExecutor;%0Aimport java.util.concurrent.TimeUnit;%0Aimport java.util.concurrent.atomic.AtomicInteger;%0A%0A/**%0A * Title : main.algorithm.leetcode %3Cbr%3E%0A * Description :%0A *%0A *%0A * @author chile%0A * @version 1.0%0A * @date 2020/11/17 15:43%0A */%0Apublic class PrintInOrder %7B%0A%0A    static class Foo %7B%0A%0A        private AtomicInteger cond;%0A%0A        private final Object lock;%0A%0A        public Foo() %7B%0A            cond = new AtomicInteger(0);%0A            lock = new Object();%0A        %7D%0A%0A        public void first(Runnable printFirst) throws InterruptedException %7B%0A            synchronized (lock) %7B%0A                while (cond.get() != 0) %7B%0A                    lock.wait();%0A                %7D%0A                // printFirst.run() outputs %22first%22. Do not change or remove this line.%0A                printFirst.run();%0A                cond.set(1);%0A                lock.notifyAll();%0A            %7D%0A        %7D%0A%0A        public void second(Runnable printSecond) throws InterruptedException %7B%0A            synchronized (lock) %7B%0A                while (cond.get() != 1) %7B%0A                    lock.wait();%0A                %7D%0A                // printSecond.run() outputs %22second%22. Do not change or remove this line.%0A                printSecond.run();%0A                cond.set(2);%0A                lock.notifyAll();%0A            %7D%0A        %7D%0A%0A        public void third(Runnable printThird) throws InterruptedException %7B%0A            synchronized (lock) %7B%0A                while (cond.get() != 2) %7B%0A                    lock.wait();%0A                %7D%0A                // printThird.run() outputs %22third%22. Do not change or remove this line.%0A                printThird.run();%0A                cond.set(3);%0A                lock.notifyAll();%0A            %7D%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void test() throws InterruptedException %7B%0A        Runnable r1 = () -%3E System.out.println(%22first%22);%0A        Runnable r2 = () -%3E System.out.println(%22second%22);%0A        Runnable r3 = () -%3E System.out.println(%22third%22);%0A        Foo foo = new Foo();%0A        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5, 60, TimeUnit.SECONDS, new ArrayBlockingQueue%3C%3E(100));%0A        Runnable task1 = () -%3E %7B%0A            try %7B%0A                foo.first(r1);%0A            %7D catch (InterruptedException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D;%0A        Runnable task2 = () -%3E %7B%0A            try %7B%0A                foo.second(r2);%0A            %7D catch (InterruptedException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D;%0A        Runnable task3 = () -%3E %7B%0A            try %7B%0A                foo.third(r3);%0A            %7D catch (InterruptedException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D;%0A        executor.submit(task1);%0A        executor.submit(task2);%0A        executor.submit(task3);%0A    %7D%0A%0A%7D%0A

print in order