Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads servicing JMS messages and use of timer/sleep

 
Les Hartzman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got threads from a newFixedThreadPool that submist a thread class that implements MessageListener. I need to also have this thread wake up every 5 seconds to check the current state of an object. Is there a better way other than using "sleep(5000)" in run() within a forever loop?

Thanks.

Les
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Hartzman wrote:I've got threads from a newFixedThreadPool that submist a thread class that implements MessageListener. I need to also have this thread wake up every 5 seconds to check the current state of an object. Is there a better way other than using "sleep(5000)" in run() within a forever loop?


Another option is to use newScheduledThreadPool() instead -- and to have the task run every five seconds. It is almost the same, except since it is the scheduler that does the potential sleep, it may do other tasks instead. Of course, your task has to be coded to only do one iteration -- no forever loop.

Henry
 
Les Hartzman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response. The problem is that the thread is also handling asynchronous JMS messages so changing it to only check messages on a periodic basis would be an issue.

Les
 
Paul Clapham
Sheriff
Pie
Posts: 20768
30
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do the two tasks absolutely have to be done by one thread? Because it looks easier to have two threads doing the two different things, and cooperating... somehow.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic