Solution:
ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
return new Thread(runnable, "MY_REPEATING_JOB");
}
});
Runnable jobToExecute = new MyJob();
scheduledExecutor.scheduleWithFixedDelay(jobToExecute, 0, 1, TimeUnit.NANOSECONDS);
I am controlling wait inside MyJob
in run()
method of Runnable
- basicaly when some operation ends I count difference time up to I want to wait, and I do sleep()
with this time.
Task is executing repeatedly, until I do somewhere in my code shutdown()
of executor.