This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes An always running process for queue processing Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "An always running process for queue processing" Watch "An always running process for queue processing" New topic
Author

An always running process for queue processing

J.H.B. Oosterlaar
Ranch Hand

Joined: Sep 12, 2002
Posts: 41
Hi,
This is the simple representation of the situation: a producer (Servlet) produces queue elements and enqueues the elements (the queue is a Singleton datastructure). A consumer dequeues the elements for processing. The produces could produce elements all the time (in a very extreme situation), and should always be able to enqueue.
The consumer is a Thread that is always running. When the queue is empty, it calls yield(), if not it dequeues the first element and processes it.
What I need to know, is it a normal Java situation, that the consumerthread is always running? The proces of enqueue and dequeue has to be asynchrone, so a consumer proces is needed to handle elements independantly.
Thanks in advance!
Jeroen
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
The Java way to to this is for the consumer to call wait(). The producer would call notify() to wake up the consumer after enqueuing a new item.
Do a search for "consumer" or "producer" in this forum and I think you'll get plenty of hi(n)ts.
- Peter
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
instead of calling yield() you could call wait(), if there is nothing to consume. Whenever a producer produces, you call notify() or notifyAll() to wake all waiting threads wake, 'because there is already resource to be consumed'.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: An always running process for queue processing
 
Similar Threads
Blocking queue Implementation
Sequential request processing
Bad Thread management (and URL decoding)
Tried out samples and order of messages not maintained
Deleting JMS Messages from the QUEUE