@@ -0,0 +1,3714 @@
+//  Copyright 2016 The Sawdust Open Source Project%0A//%0A// Licensed under the Apache License, Version 2.0 (the %22License%22);%0A// you may not use this file except in compliance with the License.%0A// You may obtain a copy of the License at%0A//%0A//     http://www.apache.org/licenses/LICENSE-2.0%0A//%0A// Unless required by applicable law or agreed to in writing, software%0A// distributed under the License is distributed on an %22AS IS%22 BASIS,%0A// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A// See the License for the specific language governing permissions and%0A// limitations under the License.%0A//%0A%0Apackage concurrent;%0A%0Aimport java.util.Stack;%0A%0Aimport static java.lang.Thread.sleep;%0A%0A/**%0A * 2 stacks work as a queue%0A * more consumers%0A * more producers%0A * make the running time of get be O(1). blocking when there is no message.%0A */%0Apublic class WaitAndNotify %7B%0A    public static void main(String args%5B%5D) %7B%0A        final int FULL_SIZE = 2;%0A        final Stack in = new Stack();%0A        final Stack out = new Stack();%0A        final Object monitor = new Object();%0A        final Object lockAllGet = new Object();%0A        final Object lockAllPut = new Object();%0A%0A        Runnable put = new Runnable() %7B%0A            private void move() %7B%0A                synchronized (in) %7B%0A                    synchronized (out) %7B%0A                        while (!in.empty()) %7B%0A                            out.push(in.pop());%0A                        %7D%0A                        System.out.println(%22moved data%22);%0A                    %7D%0A                %7D%0A            %7D%0A%0A            private void putMessage() throws InterruptedException %7B%0A                synchronized (lockAllPut) %7B%0A                    if (in.size() == FULL_SIZE) %7B%0A                        // wait till condition is met%0A                        while (in.size() == FULL_SIZE) %7B%0A                            synchronized (monitor) %7B%0A%0A                                monitor.wait();%0A                            %7D%0A                            move();%0A                        %7D%0A                    %7D%0A%0A                    synchronized (in) %7B%0A                        in.push(new java.util.Date().toString());%0A                        System.out.println(%22added%22);%0A                    %7D%0A                %7D%0A            %7D%0A%0A            @Override%0A            public void run() %7B%0A                try %7B%0A                    while (true) %7B%0A                        putMessage();%0A                    %7D%0A                %7D catch (InterruptedException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A        %7D;%0A%0A        Runnable get = new Runnable() %7B%0A            private String getMessage() throws InterruptedException %7B%0A                synchronized (lockAllGet) %7B%0A                    if (out.empty()) %7B%0A                        while (out.empty()) %7B%0A                            synchronized (monitor) %7B%0A                                monitor.notify();%0A                            %7D%0A                            sleep(10l);%0A                        %7D%0A                    %7D%0A%0A                    String message = (String) out.peek();%0A                    out.pop();%0A                    System.out.println(%22get%22);%0A                    return message;%0A                %7D%0A            %7D%0A%0A            @Override%0A            public void run() %7B%0A                try %7B%0A                    while (true) %7B%0A                        getMessage();%0A                    %7D%0A                %7D catch (InterruptedException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A        %7D;%0A%0A        new Thread(put).start();%0A        new Thread(put).start();%0A%0A        new Thread(get).start();%0A        new Thread(get).start();%0A        new Thread(get).start();%0A    %7D%0A%7D%0A

Test notify and wait in a case