Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What could cause a Java ScheduleService to not run?

In my Java application I define a ScheduleService like this:

ScheduledService<Void> scheduledService = new ScheduledService<Void>() {
    @Override
    protected Task<Void> createTask() {
        return new Task<Void>() {
            @Override
            protected Void call() {
               tick();
               return null;
            }
        };
    }
 };
scheduledService.setPeriod(new javafx.util.Duration(TICK_PERIOD.toMillis()));
scheduledService.start();

When I trigger the application from IntelliJ it works fine and tick() runs every second. When the application is packaged as an .exe using the JavaFX Packager, the service is never started.

The state of the server after I run .start() in all cases is SCHEDULED. Any ideas what else might be going on? Could something be preventing the creation of threads? Or maybe it's not switching between various threads?

The documentation for ScheduledService says (emphasis mine):

Timing for this class is not absolutely reliable. A very busy event thread might introduce some timing lag into the beginning of the execution of the background Task, so very small values for the period or delay are likely to be inaccurate. A delay or period in the hundreds of milliseconds or larger should be fairly reliable.

Is it possible there's some issue with the event thread? Is there a way to inspect it?

After calling start(), scheduleService.getExecutor() returns null. Is that expected?

I tried setting my own executor defined this way:

BlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<>();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(32, Integer.MAX_VALUE, 1000, TimeUnit.MILLISECONDS, blockingQueue);
scheduledService.setExecutor(threadPoolExecutor);

and then I print it out before and after calling start. Before it looks like this:

java.util.concurrent.ThreadPoolExecutor@4d97d155[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]

and afterwards:

java.util.concurrent.ThreadPoolExecutor@4d97d155[Running, pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 0]

So, it is claiming there's an active thread, even though it doesn't seem to be active at all.

Update: I removed the mention of a screensaver because I manage to reproduce the issue as a simple .exe but I still have the problem that the problem doesn't happen when running it from IntelliJ, it only happens when packaged as an .exe.

like image 778
pupeno Avatar asked Jan 27 '18 13:01

pupeno


1 Answers

I found the solution and it had nothing to do with ScheduleService. There were literally three other bugs in my app that were compounding to produce the unexpected behavior as well as hide my attempts at exploring the problem.

like image 198
pupeno Avatar answered Oct 16 '22 19:10

pupeno