syncstack.java

来自「由浅入深的介绍JAVAse的基本编程思想」· Java 代码 · 共 28 行

JAVA
28
字号
public class SyncStack{  //支持多线程同步操作的堆栈的实现
	private int index = 0;
	private char []data = new char[6];	
	public synchronized void push(char c){
		while(index == data.length){
		try{
				this.wait();
			}catch(InterruptedException e){}
			}
		this.notify();
		data[index] = c;
		index++;
		System.out.println("produced:"+c);
	}
	public synchronized char pop(){
		while(index ==0){
			try{
				this.wait();
			}catch(InterruptedException e){}
			    	}
		this.notify();
		index--;
		System.out.println("消费:"+data[index]);
		return data[index];
	}
}

⌨️ 快捷键说明

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