Take the 2-minute tour ×

Does anyone know if there is a reliable way to set up a producer consumer type queue where the following holds:

1) Producer initially puts three objects on queue

2) Consumer consumes one item leaving two objects on queue

3) Control passes back to Producer and Producer puts one more item on - ( now 3 objects on queue again)

4) Consumer consumes one more item and the cycle continues hence

Please note I need to set up the solution manually without using any interfaces for an assignment I am working on. Any advice would be appreciated.

Is such fine tuning available with threads?

share|improve this question
In what language? –  melpomene Nov 24 '12 at 2:42
In a way, you're talking about a semaphore. –  Hot Licks Nov 24 '12 at 2:43
Java ..Apologies, I should have mentioned that. –  M_x_r Nov 24 '12 at 2:45
If I understand this correctly: you want the Consumer to always wait until the queue is full? –  millimoose Nov 24 '12 at 3:13
@bosra Well, this is threading, there are no good practices. What you described sounds workable though. Just make sure there isn't a race condition between checking whether the queue is full and spawning a consumer. In a contrived situation where you know only the spawned consumers will be accessing the queue this should be safe. (The situation doesn't sound very practical, since to shut down a multithreaded system gracefully you generally want such queues to be emptied.) –  millimoose Nov 24 '12 at 3:30
show 3 more comments

3 Answers

What I would probably do is have a relatively mundane queue and also have a semaphore. Initialize the semaphore to -2. Have the producer increment the semaphore whenever it enqueues a request. Have the consumer decrement the semaphore prior to dequeuing a request. The consumer won't be able to do a decrement until the count gets to 1, and so there will always be 2 unserviced requests in the queue.

share|improve this answer
+1 I like this idea and it is one I will investigate further –  M_x_r Nov 24 '12 at 15:44
add comment

Java 7 has a LinkedTransferQueue which sounds like what you are looking for, or check out the parent type BlockingQueue, I'm sure one of them will fit the bill.

share|improve this answer
add comment

     You can use an ArrayList and set the capacity to 3; then, anytime the Producer executes, the array size is checked; if the size is lower than 3, simply insert values to the ArrayList till the size is 3; if the size is 3; just call the notifyAll() method to finish its turn.
      Similarly for the Consumer, if the size is 3, consume one of the values, and remove it from the ArrayList; if the size is less than 3, simply call the notifyAll() method to finish its turn.
     This is basically how it works in a nutshell; the implementation will depend on what you plan to achieve with it.

     Hope this helps.

share|improve this answer
It is probably worth adding that ArrayLists are not thread safe and all these operations would need some form of synchronization. –  assylias Nov 24 '12 at 7:28
I understand that; my assumption is that, he already has the specifics of threading covered. ArrayBlockingQueue is an example of a class he could use, if he doesn't want to use the synchronized keyword. –  Kneel-Before-ZOD Nov 24 '12 at 7:55
add comment

Your Answer


