tags:

views:

416

answers:

4

Hello

I have this problem.

I need to have a method repeated endlessly, if possible, but without overloading the system.

As I understand, while(true) { ...code... } will hang the system.

What would you suggest me to use which won't hang the system?

Thanks

Also: The execution of a my method may take up to 5 minutes, or 50 milliseconds, and I want method to be repeated jsut when it is finished

+1  A: 

You can use any scheduler.

e.g. QUARTZ is a good one.

Ritz
Schedules seem to create a new threads when one runnable is not finished (e.g. it takes ~5 minutes to be completed). and I don't want it to create a new thread, I only want it to do that when the method is finished
Andrey
Then I guess Executor is the class is you are looking for. You can create a pool of threads. Specify the number of total threads equals to 1 and you will get what you looking for.
Ritz
Executors is the class. I missed S over there.
Ritz
+3  A: 

Look at Timer and TimerTask. They are exactly what you need.

Edit: Still what you need. Look at the API's

piggles
But it seems to create a new threads when one timertask is not finished (e.g. it takes ~5 minutes to be completed). and I don't want it to create a new thread, I only want it to do that when the method is finished
Andrey
I'm afraid you don't have much of an option. If there is another way, I'd love to hear it. Best to have one separate timer dispatch thread for all your needs (as opposed to running your task on a separate thread), so as to minimize the number of threads.
piggles
+1  A: 

You could start a new Thread and run the task in the background. See the Java concurrency tutorial for more info.

If you only want to run 1 Thread for this task at a time, you could do it with a single thread executor.

Kaleb Brasee
Can I start a new thread with while(true) { ... } code inside and it won't affect main program (won't hang it) ?
Andrey
Yeah, it won't hang it -- new Threads run at the same time as the main thread. You start the new Thread, and it just runs until it finishes itself, or until your main thread stops it.
Kaleb Brasee
What kind of thread should I start for it? Could you show me a tiny bit of code please
Andrey
This page contains a code example of using a single threaded executor (to beep for an hour, every 10 seconds): http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html
Kaleb Brasee
In that example, the `final Runnable beeper` would be replaced with a class that you write. It would need to implement `Runnable`, and have a `public void run()` method that performs your scheduled method.
Kaleb Brasee
Can I set the rate to 0 ms, because I want it to be repeated again just when it is completed?
Andrey
I haven't tried it, but I assume that would work.
Kaleb Brasee
A: 

You don't need a scheduler if you want a start a new repetition as soon as the first one is done, and do it on the same thread. "while (true)" is exactly the way to do this if this is actually what you want.

Your concern about 'hanging the system" indicates that either there is other stuff happening on other threads in the program, which you want to allow to happen, or that you want this sequence to end under some condition which you haven't specified.

In the second case you need to test the termination condition in the while loop instead of "while true" (or use "break"). In the first case you need to make sure other threads get access to the CPU. Calling

Thread.currentThread().yield();

for each iteration is probably the simplest way of making sure that other threads get a chance to execute.

DJClayworth