⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buffer.java

📁 这是《Java案例精粹150例(上册)》一书配套的源代码。
💻 JAVA
字号:
package thread;

// 生产者与消费者共享的缓冲区,必须实现读、写的同步
public class Buffer {
	private int contents;
	private boolean available = false;

	public synchronized int get() {
		while (! available) {
			try {
				this.wait();
			} catch (InterruptedException exc) {}
		}
		int value = contents;
		// 消费者取出内容,改变存取控制available
		available = false;
		System.out.println("取出" + contents);
		this.notifyAll();
		return value;
	}

	public synchronized void put(int value) {
		while (available) {
			try {
				this.wait();
			} catch (InterruptedException exc) {}
		}
		contents = value;
		available = true;
		System.out.println("放入" + contents);
		this.notifyAll();
	}
}

⌨️ 快捷键说明

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