objectqueue.java

来自「Security Java Chat. This is security sim」· Java 代码 · 共 46 行

JAVA
46
字号
/*

public void run(){
	while (!isInterrupted()) {
		proceed(queue.get());
	}
}
private void proceed(Object obj) {
}
*/

//an optimized version of this class is available
package sjc;
import java.util.*;
public class ObjectQueue {
	private LinkedList queue = null;
	private volatile int objects = 0;
	public ObjectQueue() {
		queue = new LinkedList();
	}
	public synchronized Object get() {
		try {
			while (objects == 0) {
				notifyAll();
				wait(1000);
			}
			objects--;
			return queue.removeLast();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public synchronized void put(Object obj) {
		try {
			notifyAll();
			queue.addFirst(obj);
			objects++;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

⌨️ 快捷键说明

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