cubbyhole.java

来自「Java Thread Programming (Source」· Java 代码 · 共 52 行

JAVA
52
字号
public class CubbyHole extends Object {
	private Object slot;

	public CubbyHole() {
		slot = null; // null indicates empty
	}

	public synchronized void putIn(Object obj) 
						throws InterruptedException {

		print("in putIn() - entering");

		while ( slot != null ) {
			print("in putIn() - occupied, about to wait()");
			wait(); // wait while slot is occupied
			print("in putIn() - notified, back from wait()");
		}

		slot = obj;  // put object into slot
		print("in putIn() - filled slot, about to notifyAll()");
		notifyAll(); // signal that slot has been filled

		print("in putIn() - leaving");
	}

	public synchronized Object takeOut() 
						throws InterruptedException {

		print("in takeOut() - entering");

		while ( slot == null ) {
			print("in takeOut() - empty, about to wait()");
			wait(); // wait while slot is empty
			print("in takeOut() - notified, back from wait()");
		}

		Object obj = slot;
		slot = null; // mark slot as empty
		print(
			"in takeOut() - emptied slot, about to notifyAll()");
		notifyAll(); // signal that slot is empty

		print("in takeOut() - leaving");
		return obj;
	}

	private static void print(String msg) {
		String name = Thread.currentThread().getName();
		System.out.println(name + ": " + msg);
	}
}

⌨️ 快捷键说明

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