在java中使用简单数组[]实现队列
我曾想过在java中使用简单数组[]实现一个队列。我希望所有可能使用的案例都是我应该考虑解决的。 我实现它的方法是在当前put()值索引位置上保留一个put引用指针。以及指向当前poll()值索引位置的轮询引用指针 我考虑的一个可能的用例: 1.>如果当前推送和轮询引用指针指向同一位置,则不会执行poll()操作在java中使用简单数组[]实现队列,java,arrays,queue,Java,Arrays,Queue,我曾想过在java中使用简单数组[]实现一个队列。我希望所有可能使用的案例都是我应该考虑解决的。 我实现它的方法是在当前put()值索引位置上保留一个put引用指针。以及指向当前poll()值索引位置的轮询引用指针 我考虑的一个可能的用例: 1.>如果当前推送和轮询引用指针指向同一位置,则不会执行poll()操作 其他的用例我应该考虑。 < P > 1)你应该考虑如果队列满了,就不允许放置。 2) 您可能有办法检索队列的大小,即队列中的元素数 3)如果要在线程间使用队列,则应该考虑线程安全性。
其他的用例我应该考虑。
< P > 1)你应该考虑如果队列满了,就不允许放置。 2) 您可能有办法检索队列的大小,即队列中的元素数3)如果要在线程间使用队列,则应该考虑线程安全性。
4) 您可能需要添加其他操作,如peek() 编辑 显而易见的队列实现如下所示:public class ArrayQueue {
/**
* Array to store queue elements.
*/
private Object [] queueArray;
/**
* Size of above array.
*/
private int arraySize;
/**
* Position in array where next queue element will be inserted.
*/
private int putPosition;
/**
* Position in array from which next queue element will be retrieved.
*/
private int getPosition;
/**
* Current number of elements in queue.
*/
private int currentSize;
/**
* Lock for thread safety.
*/
private final Object lock = new Object();
public ArrayQueue(int size){
if(size <= 0){
// there must be queue of at least 1 element.
throw new IllegalArgumentException();
}
// initialize other things.
queueArray = new Object[size];
arraySize = size;
putPosition = 0;
getPosition = 0;
currentSize = 0;
}
/**
* Inserts element in queue.
* If queue if full, then waits for queue to make empty room for element to insert.
* @param object to insert.
* @throws InterruptedException
*/
public void put(Object object) throws InterruptedException {
synchronized (lock) {
// critical section.
while(currentSize == arraySize){
// queue is full, wait for someone to call get() to have empty room in queue.
lock.wait();
}
// we have empty room in queue.
// insert next element in queue.
queueArray[putPosition] = object;
// update position where next element will be inserted.
putPosition = putPosition + 1;
if(putPosition == arraySize){
// we reached end of index bound, wrap index back to 0.
putPosition = 0;
}
// update current size.
currentSize = currentSize + 1;
if(currentSize == 1){
// queue was empty before, invoke any calls if blocked in get().
lock.notify();
}
}
}
/**
* Returns next element from queue.
* If queue is empty, waits for some element to be added in queue.
* @return next element.
* @throws InterruptedException
*/
public Object get() throws InterruptedException {
synchronized (lock) {
// critical section.
while(currentSize == 0){
// queue is empty, wait.
lock.wait();
}
// queue is not empty.
// retrieve next element.
Object object = queueArray[getPosition];
// update position from where next element will be retreived.
getPosition = getPosition + 1;
if(getPosition == arraySize){
getPosition = 0;
}
if(currentSize == arraySize){
// queue was full before, invoke any calls if blocked in put().
lock.notify();
}
// update current size.
currentSize = currentSize - 1;
// return element.
return object;
}
}
public int size(){
synchronized (lock) {
return currentSize;
}
}
}
公共类数组队列{
/**
*数组来存储队列元素。
*/
私有对象[]队列数组;
/**
*以上数组的大小。
*/
私有内部阵列化;
/**
*数组中插入下一个队列元素的位置。
*/
私人职位;
/**
*从中检索下一个队列元素的数组中的位置。
*/
私人职位;
/**
*队列中的当前元素数。
*/
私有int-currentSize;
/**
*螺纹安全锁。
*/
私有最终对象锁=新对象();
公共阵列队列(整数大小){
if(size)您想以多线程方式使用队列吗?为什么要对队列使用数组而不只是使用队列?任何ADT(抽象数据类型)使用底层基本数据结构,如java提供的数组或基于节点的实现。Thanx作为答复,我需要固定大小的队列实现。您给出的所有考虑事项都是队列的规范/定义。我需要在使用数组作为队列时,基于核心级实现的考虑事项