drop.java

来自「JAVA 工作指南 可以说是程序员必备的东西哦」· Java 代码 · 共 37 行

JAVA
37
字号
public class Drop {    //Message sent from producer to consumer.    private String message;    //True if consumer should wait for producer to send message, false    //if producer should wait for consumer to retrieve message.    private boolean empty = true;    public synchronized String take() {        //Wait until message is available.        while (empty) {            try {                wait();            } catch (InterruptedException e) {}        }        //Toggle status.        empty = true;        //Notify producer that status has changed.        notifyAll();        return message;    }    public synchronized void put(String message) {        //Wait until message has been retrieved.        while (!empty) {            try {                 wait();            } catch (InterruptedException e) {}        }        //Toggle status.        empty = false;        //Store message.        this.message = message;        //Notify consumer that status has changed.        notifyAll();    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?