File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Singleton with a single worker thread Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Singleton with a single worker thread" Watch "Singleton with a single worker thread" New topic
Author

Singleton with a single worker thread

Ziphyre Marco
Greenhorn

Joined: Dec 18, 2008
Posts: 3
Hello,

In a Servlet environment I try to implement a Messenger class for the jobs of taking messages to its queue and sending them to online users. Messenger is a Singleton implementing Runnable. I can't find a proper way to .start() the inner thread for checking the queue and sending the message (in an another thread). Can you please comment on the design, possible caveats and a way to .start()



What I try is to use the Messenger class as:
Messenger.getInstance().addMessageToQueue(...);

And than the inner polling thread get messages from the queue and send them
Thanks
Evan Caballero
Ranch Hand

Joined: Dec 10, 2009
Posts: 59
take a look at the java.util.concurrent API. Instantiate a ConcurrentLinkedQueue or something like that, and "plug" it to a ThreadPool. Each message added to the queue will be automatically consumed by the pool.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Singleton with a single worker thread
 
Similar Threads
ConcurrentHapMap size not as expected when put done using Multiple Threads
HI Help synchronized
please Explain This question
Phantom Reference
Multithreaded Example... Where to put WAIT() , NOTIFY() advise please