Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheduling java process for a specific time interval with a given delay

We want to schedule a java process to run till a specific time interval. Currently I am thinking to using TimerTask to schedule this process. In the start of every loop, will check the current time and then compare with the given time and stop the process if the time is elapsed. Our code is something like below:

import java.util.Timer;
import java.util.TimerTask;

public class Scheduler extends TimerTask{

    public void run(){
        //compare with a given time, with getCurrentTime , and do a System.exit(0);
        System.out.println("Output");
    }

    public static void main(String[] args) {
        Scheduler scheduler = new Scheduler();
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(scheduler, 0, 1000);

    }

}

Is there a better approach for this?

like image 497
Anupam Avatar asked Jun 09 '15 10:06

Anupam


2 Answers

You can use RxJava, a very powerful library for reactive programming.

Observable t =  Observable.timer(0, 1000, TimeUnit.MILLISECONDS);
t.subscribe(new Action1() {
                        @Override
                        public void call(Object o) {
                            System.out.println("Hi "+o);
                        }
                    }

) ;
try {
    Thread.sleep(10000);
}catch(Exception e){ }

You can even use the lambda syntax:

Observable t =  Observable.timer(0, 1000, TimeUnit.MILLISECONDS);
t.forEach(it -> System.out.println("Hi " + it));
try {
    Thread.sleep(10000);
}catch(Exception e){  }
like image 20
frhack Avatar answered Sep 27 '22 22:09

frhack


Instead of checking if the time limit has been reached in every single iteration you could schedule another task for the said time limit and call cancel on your timer.

Depending on the complexity you might consider using a ScheduledExecutorService such as ScheduledThreadPoolExecutor. See in this answer when and why.

Simple working example with timer:

public class App {
    public static void main(String[] args) {
        final Timer timer = new Timer();
        Timer stopTaskTimer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                System.out.println("Output");
            }
        };
        TimerTask stopTask = new TimerTask() {
            @Override
            public void run() {
                timer.cancel();
            }
        };

        //schedule your repetitive task
        timer.scheduleAtFixedRate(task, 0, 1000);
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse("2015-06-09 14:06:30");
            //schedule when to stop it
            stopTaskTimer.schedule(stopTask, date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
like image 128
Laurentiu L. Avatar answered Sep 27 '22 22:09

Laurentiu L.