3

Currently, using cron4j, i am able to schedule execution of an event at, say 13:01. While fine, from what i understand it does not allow one to schedule an event at 13:01:10 (10 seconds after).

Is there a tool that allows such granularity in scheduling?

James Raitsev
  • 92,517
  • 154
  • 335
  • 470
  • Unfortunately using cron4j seems impossible. But you can do something like this: http://stackoverflow.com/a/26143988/450148 – Felipe Oct 08 '15 at 08:32

4 Answers4

1

If you're looking for a library, you should look into Quartz. It is a very flexible scheduler.

Brigham
  • 14,395
  • 3
  • 38
  • 48
  • And it can do sub-minute scheduling. Here's an example of [a schedule for running every ten seconds](http://quartz-scheduler.org/documentation/quartz-2.1.x/cookbook/TenSecTrigger). – Tom Anderson Aug 31 '12 at 19:52
1

In Java you can schedule task using Timer and Timer task class for example demo

import java.util.Timer;
import java.util.TimerTask;
public class MyTask extends TimerTask{
    Timer timer;
    int count=0;
    public MyTask(){        
    }
    public MyTask(Timer timer){
        this.timer=timer;
    }
    public void toDo(){
        System.out.println("count-> "+(count++));        
    }
    @Override
    public void run() {
        toDo();
        if(count>10){//this is the condition when you want to stop the task.
            timer.cancel();
        }
    }
}

for this timer you can run this as follows

public static void main(String[] args){
     Timer timer=new Timer();
     MyTask myTask=new MyTask(timer);
     int firstSart=1000;// it means after 1 second.
     int period=1000*2;//after which the task repeat;
     timer.schedule(myTask,firstSart,period);//the time specified in millisecond.     
 }
sumit sharma
  • 1,067
  • 8
  • 24
0

How about the Timer class? It allows to schedule executions in regular intervals which are accurate to the millisecond.

Philipp
  • 67,764
  • 9
  • 118
  • 153
-1

I personally find quartz a very complicated and strong scheduler so It's not easy to get started with quickly. It may not be an effective solution but you can sleep(long) before the execution of a task scheduled every minute.

public void execute(TaskExecutionContext context){
    sleep(TimeUnit.SECONDS.toMilliseconds(10));
}

I haven't checked the syntax for the previous code so you might need to validate that.

Muhammad Gelbana
  • 3,890
  • 3
  • 43
  • 81