9

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.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
  • may i know why you return null in call method which return type is Void? – Shen Yudong Jan 27 '18 at 13:40
  • @yudongshen: what else could I return? – Pablo Fernandez Jan 27 '18 at 13:41
  • you do not need return at all. – Shen Yudong Jan 27 '18 at 13:43
  • 1
    @yudongshen: my compiler disagrees: `Error:(89, 21) java: missing return statement` – Pablo Fernandez Jan 27 '18 at 13:44
  • you right. it is Void, not void. need a return value. – Shen Yudong Jan 27 '18 at 13:50
  • If you're using Windows, is there a `PATHEXT` environment variable `SCR`? – zlakad Jan 27 '18 at 14:04
  • @zlakad: yes, I'm using Windows. I don't exactly know what you mean by the question. I know what environment variables are but not what a `PATHEXT` environment variable is. – Pablo Fernandez Jan 27 '18 at 15:42
  • @zlakad: there's an environment variable called `PATHEXT` with this content: `PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC` – Pablo Fernandez Jan 27 '18 at 15:43
  • Why don't you add `.SCR` to this `PATHEX` and see what happens - these are the "runnable" extensions. – zlakad Jan 27 '18 at 15:45
  • @zlakad: asking my customers to modify their environment variables to use my product seems a lot to ask. Not having given it there is not preventing this or any other screensaver from running. Is there a reason why you believe this environment variable would have an effect o the execution of a SchedulesService? – Pablo Fernandez Jan 27 '18 at 15:49
  • To be honest, I don't know for sure - but it came to my mind... If other screen savers CAN run, then I am very likely wrong. – zlakad Jan 27 '18 at 15:52
  • My screensaver is running as well. Just not spawning background threads or something like that. – Pablo Fernandez Jan 27 '18 at 15:53
  • Oh, well.. I'm sorry, I cannot help you. Good luck, friend. – zlakad Jan 27 '18 at 15:54
  • You seem to be fixated on the `ScheduledService`. What if that's not the issue? Check [this question](https://stackoverflow.com/questions/2135982/turn-java-app-into-windows-screensaver) – Adelin Jan 29 '18 at 14:10
  • @Adelin: I'm not fixating on it, it's the bit that doesn't work. I checked that question and it doesn't add anything to the problem I'm experiencing. Can you elaborate what you mean? – Pablo Fernandez Jan 29 '18 at 14:12
  • I mean - is your program accepting those command line args? It seems that's the criteria for the java app to be executed as a screensaver – Adelin Jan 29 '18 at 14:13
  • @Adelin: yes, it accepts those arguments and does everything else a screensaver needs. It is a working screensaver already, but the `ScheduledService` is not being triggered. – Pablo Fernandez Jan 29 '18 at 14:14
  • Can you take a thread dump and see if something is blocking at the top of the stack? – Paul MacGuiheen Jan 29 '18 at 14:20
  • How do I take this thread dump? – Pablo Fernandez Jan 29 '18 at 14:21
  • Would JVisualVM or JConsole not work? – Paul MacGuiheen Jan 29 '18 at 14:42
  • @PaulMacGuiheen: No, because the error only happens when my code is being executed as a screensaver, so, I cannot interact with the computer in any way while the error is happening. I'm trying to reproduce it without being in screensaver mode, but so far I haven't succeeded (and I suspect if I succeed I will have found the problem and possibly the solution). – Pablo Fernandez Jan 29 '18 at 14:44
  • Maybe you can add some code to help out https://stackoverflow.com/questions/3958938/how-do-i-create-a-thread-dump-via-jmx – Paul MacGuiheen Jan 29 '18 at 14:49
  • I may have found the solution. I'll post more soon. – Pablo Fernandez Jan 29 '18 at 16:41

1 Answers1

1

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.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622