@@ -0,0 +1,1691 @@
+package leetcode.concurrency;%0A%0Apublic class FooBar %7B%0A%0A    private int n;%0A    private volatile String nextPrint = %22foo%22;%0A%0A    public FooBar(int n) %7B%0A        this.n = n;%0A    %7D%0A%0A    public void foo(Runnable printFoo) throws InterruptedException %7B%0A        for (int i = 0; i %3C n; i++) %7B%0A            while (true) %7B%0A                if (nextPrint.equals(%22foo%22)) %7B%0A                    printFoo.run();%0A                    nextPrint = %22bar%22;%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    public void bar(Runnable printBar) throws InterruptedException %7B%0A        for (int i = 0; i %3C n; i++) %7B%0A            while (true) %7B%0A                if (nextPrint.equals(%22bar%22)) %7B%0A                    printBar.run();%0A                    nextPrint = %22foo%22;%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) throws InterruptedException %7B%0A        FooBar fooBar = new FooBar(2);%0A        Thread foo = new Thread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                try %7B%0A                    fooBar.foo(() -%3E System.out.print(%22foo%22));%0A                %7D catch (InterruptedException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A        %7D);%0A        Thread bar = new Thread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                try %7B%0A                    fooBar.bar(() -%3E System.out.print(%22bar%22));%0A                %7D catch (InterruptedException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A        %7D);%0A%0A        foo.start();%0A        bar.start();%0A%0A        foo.join();%0A        bar.join();%0A    %7D%0A%7D%0A

Add FooBar