• Post Reply Bookmark Topic Watch Topic
  • New Topic

An always running process for queue processing

 
J.H.B. Oosterlaar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!