Queue
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   BW_T
Posted On:   Tuesday, October 11, 2005 04:57 AM

I have 2 threads – a producer and a consumer. The producer is expected to produce faster than the consumer can consume – although it is not always the case. In order to make sure that the consumer consumes the messages in order (FIFO), I plan to use a queue. I am happy that Java 1.5 has introduced a bunch of Queue classes and interfaces into its group of Collection classes and interfaces. However, there are so many queue implementation classes – which one should I use ? Should I use a blocking queue ? Is there a way to prevent the consumer from keep on checking on the contents of the queue ? Will the “blocking” feature address this ? How do I make sure that my   More>>

I have 2 threads – a producer and a consumer. The producer is expected to produce faster than the consumer can consume – although it is not always the case.



In order to make sure that the consumer consumes the messages in order (FIFO), I plan to use a queue. I am happy that Java 1.5 has introduced a bunch of Queue classes and interfaces into its group of Collection classes and interfaces.



However, there are so many queue implementation classes – which one should I use ?



Should I use a blocking queue ?

Is there a way to prevent the consumer from keep on checking on the contents of the queue ? Will the “blocking” feature address this ?

How do I make sure that my queue is accessed in a thread safe way ? Or is it already thread-safe by default ?

   <<Less

Re: Queue

Posted By:   Jani_Kaarela  
Posted On:   Wednesday, October 12, 2005 03:20 AM



Is there a way to prevent the consumer from keep on checking on the contents of the queue ? Will the “blocking” feature address this ?
How do I make sure that my queue is accessed in a thread safe way ? Or is it already thread-safe by default ?

Yes, "blocking" means exactly that the consumer thread will wait for queue content to become available. Blocking queues are also thread-safe. See java.util.concurrent.BlockingQueue.

Re: Queue

Posted By:   WarnerJan_Veldhuis  
Posted On:   Tuesday, October 11, 2005 05:11 AM

That is what Object.wait() and Object.notify() was made for. A consumer waits for a signal from the queue. Once the producer puts something in the queue, the consumers are notified. See here (The third page talks about wait()/notify())
About | Sitemap | Contact