messagequeue.java
来自「基于jxta的P2P框架的系统」· Java 代码 · 共 75 行
JAVA
75 行
package connex.plugins.whiteboard;
import java.util.Vector;
import net.jxta.endpoint.Message;
class MessageQueue extends Thread {
private Vector messages = new Vector();
/**
* @directed
*/
private MessageProcessor mprocessor;
private boolean start = true;
protected MessageQueue(MessageProcessor mprocessor) {
this.mprocessor = mprocessor;
}
protected void stopIt() {
start = false;
}
public void run() {
Message message = null;
while (start) {
try {
message = pop();
} catch (InterruptedException e) {
break;
}
if (message != null) {
mprocessor.process(message);
}
}
}
public synchronized void push(Message message) {
boolean shouldNotify = (messages.size() == 0);
// add the message to the end of the vector
messages.addElement(message);
if (shouldNotify) {
// notify the thread
notify();
}
}
private synchronized Message pop() throws InterruptedException {
Message message = null;
if (messages.size() == 0) {
// wait for an element
wait();
}
// pop the first element off of the vector
if (messages.size() > 0) {
message = (Message) messages.firstElement();
messages.removeElementAt(0);
}
return message;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?