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

Constant running process

 
Tom Henricksen
Ranch Hand
Posts: 136
Eclipse IDE jQuery Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have a process we want to run constantly. Once finish sleep for awhile then come back and run against.



This seems to be the right way but I am not that experienced in threading.
Thanks,
Tom
 
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
This seems to be the right way but I am not that experienced in threading.


Tom,

I recommend that you take a look at Sun's threading tutorial. The example you provided does not start any new threads whatsoever.

Here is a possible fix to your orig program...



Hmmmm... actually, here is probably a better version...



Henry
 
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
Tom,

I might have misread your question. You never stated that you wanted to create new threads. And this question may just be how to use the sleep() method of the Thread class.

If this is true... The sleep() method is static and affects the current thread. There is no reason to create a new thread object -- it can be done like this...



Henry
 
Andrew Sancroff
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pandora.getBox.open();

There are so many issues to consider. You need to start by reading chapter 14 of The Java Programming Language, 4th edition. Section 14.10 (including all its subsections) is the real mind-blower (the Java memory model).
 
Ajay Saxena
Ranch Hand
Posts: 154
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no reason to create a new thread object -- it can be done like this...


code:
--------------------------------------------------------------------------------

while (true) { try { service.processNow(); Thread.sleep(1000); } catch (InterruptedException e) {} }

--------------------------------------------------------------------------------






This is what I was expecting as an answer to this query..finally it emerged :-)
 
Tom Henricksen
Ranch Hand
Posts: 136
Eclipse IDE jQuery Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to create a new thread. We currently have a web application where we do create a Timer. Our concern is with the Timer object it will wake up run when or original Timer object is still running. Our task could run for a long time so looking at the javadoc a "Timer tasks should complete quickly."
Timer Javadoc
So I would like to create a new thread when our web application starts and have that keep processing then sleep. We would only need one thread to do this.

I tried just calling Thread.sleep() but then my web application never started.
Then I realized I need a separate thread for this...

Thanks,
Tom
 
Tom Henricksen
Ranch Hand
Posts: 136
Eclipse IDE jQuery Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used Henry suggestion and everything is working as I wanted. Thanks everyone for all your help.
Tom
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic