wait and notify in Synchronization in Java


wait and notify/notifyAll are Object methods used for coordinating the execution of thread instances.

Source Code

class Message {
    private String msg;
    public synchronized void put(String msg) {
        this.msg = msg;
        notify();
    }
    public synchronized String take() {
        while (msg == null) {
            try {
                wait();
            } catch (InterruptedException e) {}
        }
        String returnMsg = msg;
        msg = null;
        return returnMsg;
    }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments