cubbyhole.java

来自「本压缩文件中含有线程的控制」· Java 代码 · 共 57 行

JAVA
57
字号
package thread;

public class CubbyHole {

	private boolean available;

	private int content;
	
	public synchronized void put(int c){
		if(!available){
			content = c;
			available = true;
			System.out.println("Producer put " + c);
		}
	}
	
	public synchronized int get(){
		if(available){
			available = false;
			System.out.println("consumer get " + this.content);
			return content;
			
		}
		return -1;
	}
//
//	public synchronized void put(int c) {
//		if (available) {
//			try {
//				wait();
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//		content = c;
//		available = true;
//		notify();
//	}
//
//	public synchronized int get() {
//		if (!available) {
//			try {
//				wait();
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//		available = false;
//		notify();
//		return content;
//
//	}

}

⌨️ 快捷键说明

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