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 Constant running process Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Constant running process" Watch "Constant running process" New topic
Author

Constant running process

Tom Henricksen
Ranch Hand

Joined: Mar 23, 2004
Posts: 136

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
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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

Joined: Dec 25, 2006
Posts: 10
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

Joined: Nov 13, 2006
Posts: 154
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

Joined: Mar 23, 2004
Posts: 136

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

Joined: Mar 23, 2004
Posts: 136

I used Henry suggestion and everything is working as I wanted. Thanks everyone for all your help.
Tom
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constant running process
 
Similar Threads
Threads
What is the difference between process & thread??
Difference between run and start method of Thread
join()
threads