pup*_*eno 9 java service multithreading
在我的Java应用程序中,我定义了一个ScheduleService,如下所示:
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();
Run Code Online (Sandbox Code Playgroud)
当我从IntelliJ触发应用程序时,它工作正常,tick()每秒运行一次.当应用程序打包为.exe使用JavaFX Packager时,该服务永远不会启动.
.start()在所有情况下运行后服务器的状态是SCHEDULED.还有什么想法可能会发生什么?有什么东西可以防止创建线程吗?或者它可能不是在各种线程之间切换?
ScheduledService说的文件(强调我的):
这个课程的时间安排并不绝对可靠.非常繁忙的事件线程可能会在后台任务执行开始时引入一些时间延迟,因此周期或延迟的非常小的值可能不准确.数百毫秒或更大的延迟或周期应该是相当可靠的.
事件线程有可能存在问题吗?有没有办法检查它?
打电话后start(),scheduleService.getExecutor()退货null.这是预期的吗?
我尝试以这种方式设置我自己的执行器:
BlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<>();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(32, Integer.MAX_VALUE, 1000, TimeUnit.MILLISECONDS, blockingQueue);
scheduledService.setExecutor(threadPoolExecutor);
Run Code Online (Sandbox Code Playgroud)
然后我在调用start之前和之后打印出来.在它看起来像这样之前:
java.util.concurrent.ThreadPoolExecutor@4d97d155[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
Run Code Online (Sandbox Code Playgroud)
然后:
java.util.concurrent.ThreadPoolExecutor@4d97d155[Running, pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 0]
Run Code Online (Sandbox Code Playgroud)
所以,它声称有一个活跃的线程,即使它似乎根本不活跃.
更新:我删除了屏幕保护程序的提及,因为我设法将问题重现为一个简单.exe但我仍然有问题,从IntelliJ运行它时不会发生问题,它只会在打包时发生.exe.
我找到了解决方案,它与 无关ScheduleService。我的应用程序中实际上还存在其他三个错误,这些错误综合起来产生了意想不到的行为,并隐藏了我探索问题的尝试。