• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton with a single worker thread

 
Ziphyre Marco
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!