Posted By:
Chris_Lumpkin
Posted On:
Friday, April 19, 2002 08:40 AM
I'm developing a multithreaded application which uses queues to store tasks - a pretty common solution to multithreading with data. I looked at several examples of blocking queues that block threads when they try to remove Objects from the queue when it is empty. I also have a need for the queue to allow a maximum size to be set, such that the queue will also block a thread trying to add an Object to the queue, if the queue has reached the maximum size. I implemented the enqueue method the same as the dequeue methods I have seen in examples. The threads that get blocked on dequeue with an empty queue will return to running when more Objects are added to the queue, but threads blocked on e
More>>
I'm developing a multithreaded application which uses queues to store tasks - a pretty common solution to multithreading with data. I looked at several examples of blocking queues that block threads when they try to remove Objects from the queue when it is empty.
I also have a need for the queue to allow a maximum size to be set, such that the queue will also block a thread trying to
add
an Object to the queue, if the queue has reached the maximum size.
I implemented the
enqueue
method the same as the
dequeue
methods I have seen in examples. The threads that get blocked on
dequeue
with an empty queue will return to running when more Objects are added to the queue, but threads blocked on
enqueue
due to the queue having reached max size will never return from
wait
. Here is an excerpt of
enqueue
:
/**
* Enqueue an object
*/
public final synchronized void enqueue(Object _item)
throws InterruptedException, QueueClosedException {
if(queueClosed) {
throw new QueueClosedException();
}
// Block Threads trying to enqueue when it will
//cause the queue to grow beyond the max size.
if(queue.size() >= getMaxSize()) {
while(queue.size() >= getMaxSize()) {
wait();
if(queueClosed) {
throw new QueueClosedException();
}
}
}
queue.addLast(_item);
notifyAll();
}
Any help is greatly appreciated. Thanks,
Chris Lumpkin
<<Less