I have a singleton class that creates a thread on instance creation. Only one object of this class is created and the thread is scheduled to run every 12 hours and print the given statement. In some cases after the first 12h the thread is invoked every 4-5 seconds. I am confused as to why this happens as I have configured the scheduler to run once every 12 hours and not every 4-5 seconds. Is there any config I have missed or is the scheduler wrong configured?
the singleton class,
import java.time.Instant;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
public class ThreadSchedulerTest {
private static final ThreadSchedulerTest test = new ThreadSchedulerTest();
private ThreadSchedulerTest (){createThread();}
private void createThread() {
Runnable renewer = () -> {
System.out.println("Running ok , time is : "+ Instant.now());
};
Executors.newSingleThreadScheduledExecutor(new
ThreadSchedulerThreadFactory("thread1")).scheduleAtFixedRate(
renewer, 0, 12, TimeUnit.HOURS);
}
static class ThreadSchedulerThreadFactory implements ThreadFactory {
private final String name;
public ThreadSchedulerThreadFactory(final String name) {
this.name = name;
}
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r, name);
thread.setDaemon(true);
return thread;
}
}
}
Can someone please help clarify this issue?