File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads servicing JMS messages and use of timer/sleep

 
Les Hartzman
Ranch Hand
Posts: 35
  • 0
  • 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: 20817
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • 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
  • 0
  • 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: 20155
23
MySQL Database
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic