0

I need to make a Thread which starts it's work at the time the program runs and dies when the program is closed. this Thread is going to check something every 1 minute ones.

what is the best way for this scheduling, using Thread.sleep() or using a Timer or what?

Soheil
  • 1,676
  • 7
  • 34
  • 65
  • 6
    `Executors.newScheduledThreadPool`, probably. – Louis Wasserman Jan 24 '13 at 20:42
  • and how to use that, how can I tell execute this `Runnable` every 1 minute for example ? – Soheil Jan 24 '13 at 20:46
  • 2
    Look at the Javadoc. `ScheduledExecutorService` has a [method](http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html#scheduleAtFixedRate(java.lang.Runnable,%20long,%20long,%20java.util.concurrent.TimeUnit)) that lets you do exactly that. – Louis Wasserman Jan 24 '13 at 20:47
  • 2
    @Soheil look at the java docs for ScheduledExecutorService, there's a choice between "fixed rate" (start every minute) and "fixed delay" (start one minute after the last execution finished). – Ian Roberts Jan 24 '13 at 20:48
  • I think the best is to use a TimerTask, take a look in this post http://stackoverflow.com/questions/14454063/how-to-make-a-timer/14454211#14454211 – fmodos Jan 24 '13 at 20:49
  • @fmodos `Timer`s and `TimerTask`s are for dispatching timed events to the Swing GUI thread, not really general-purpose. (Of course it might be the OP wants to do exactly that, I'm just clarifying.) – millimoose Jan 24 '13 at 20:52
  • @fmodos I was thinking about that, too but now I think using `Executors.newScheduledThreadPool` will be better – Soheil Jan 24 '13 at 20:53
  • @millimoose `java.util.Timer` is not for GUI purposes – Soheil Jan 24 '13 at 20:55
  • Executors appeared first in 1.5. Timer was the fancy way of doing it before that (starting in 1.3). There is another Timer class as part of Swing. – Ralf H Jan 24 '13 at 20:58
  • @Soheil Ah. I misread the linked question, sorry. – millimoose Jan 24 '13 at 21:09

3 Answers3

3

You've not provided any code, but there's loads of examples about for this sort of thing, here's one:

import static java.util.concurrent.TimeUnit.*;
class BeeperControl {
private final ScheduledExecutorService scheduler =
       Executors.newScheduledThreadPool(1);

    public void beepForAnHour() {
        final Runnable beeper = new Runnable() {
                public void run() { System.out.println("beep"); }
            };
        final ScheduledFuture<?> beeperHandle =
            scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
        scheduler.schedule(new Runnable() {
                public void run() { beeperHandle.cancel(true); }
            }, 60 * 60, SECONDS);
    }
 }

http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html#scheduleAtFixedRate(java.lang.Runnable

Mark
  • 2,423
  • 4
  • 24
  • 40
2

So you will have something like

public class Application {
     private final ScheduledExecutorService executor;
     private final Runnable task;

     public Application(ScheduledExecutorService executor, Runnable task) {
         this.executor = executor;
         this.task = task;
     }

     public void init() {
         executor.scheduleAtFixedRate(task, 0, 60, TimeUnit.SECONDS);
     }
     public void shutdown() {
         executor.shutdownNow();
     }
}

and you will create your application with something like

 // ....
 Application app = new Application(Executors.newSingleThreadScheduledExecutor(), task);
 app.init();
 // ....
 // at end
 app.shutdown();
Caesar Ralf
  • 2,203
  • 1
  • 18
  • 36
0

To make a Thread which starts it's work at the time the program runs and dies when the program is closed, mark this thread as a daemon thread:

Thread myThread=new Thread();
myThread.setDaemon(true);
myThread.start(); // forget about it, no need to explicitly kill it
Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38