simplemessagequeue.java

来自「java版ace,java程序员值得一看」· Java 代码 · 共 87 行

JAVA
87
字号
package JACE.tests.Concurrency.Condition;import JACE.ASX.TimeoutException;import JACE.ASX.TimeValue;import JACE.Concurrency.*;public class SimpleMessageQueue{  private int num_items_ = 0;  private int head_ = 0, tail_ = 0;  private Object[] queue_;  private Mutex lock_ = new Mutex ();  private Condition notFull_ = new Condition (lock_);  private Condition notEmpty_ = new Condition (lock_);    public SimpleMessageQueue(int size)  {    queue_ = new Object[size];  }    public void enqueue(Object element, TimeValue timeout)    throws TimeoutException, InterruptedException  {    try       {	lock_.acquire ();    	while (this.isFull ())	  notFull_.Wait (timeout);		if (tail_ == queue_.length)	  tail_ = 0;	queue_[tail_] = element;	tail_++;	num_items_++;	notEmpty_.signal ();      }    finally      {	lock_.release ();      }  }  public Object dequeue (TimeValue timeout)        throws TimeoutException, InterruptedException  {    Object return_value = null;	    try       {	lock_.acquire ();	while (this.isEmpty ())	  notEmpty_.Wait (timeout);		return_value = queue_[head_];	head_++;	if (head_ == queue_.length)	  head_ = 0;	num_items_--;    	notFull_.signal ();      }    finally       {	lock_.release ();      }    return return_value;  }  public boolean isEmpty()  {    return num_items_ == 0;  }  public boolean isFull()  {    return num_items_ == queue_.length;  }  public int size()  {    return num_items_;  }}

⌨️ 快捷键说明

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