简体   繁体   English

Java ScheduledThreadPool 多线程在一段时间后停止

[英]Java ScheduledThreadPool multithreads stops after some time

I´m developing a J2SE app that sync GPS information of 184 cars stored at four databases on differents servers.我正在开发一个 J2SE 应用程序,它可以同步存储在不同服务器上的四个数据库中的 184 辆汽车的 GPS 信息。 For every car I create a Thread and stored in a ScheduledThreadPool我为每辆车创建一个线程并存储在 ScheduledThreadPool

    ScheduledExecutorService executor = Executors.newScheduledThreadPool(20);

    for (int i = 0; i < cars.size(); i++) {
        Car vec_temp = cars.get(i);
        SyncThread carThread = new SyncThread(q, vec_temp, reintentos);
        long sleep = carThread.calculateVehicleDelay();
        executor.scheduleAtFixedRate(hilo, 0, sleep, MILLISECONDS);
    }

Information of Latitude and Longitude is retrieved by a SocketServer in XML data, so inside the SyncThread Class I instanciate a Socket and ask for information and close that connection, and make this every 15 minutes...纬度和经度信息由 XML 数据中的 SocketServer 检索,因此在 SyncThread Class 中,我实例化了一个 Socket 并询问信息并关闭该连接,并每 15 分钟进行一次...

The app start working great for some reason at some time stops running with no Exception...该应用程序由于某种原因开始运行良好,有时会停止运行,没有异常......

The jar is running as a windows service. jar 作为 windows 服务运行。

According to the API ,根据API

If any execution of the task encounters an exception, subsequent executions are suppressed.如果任务的任何执行遇到异常,则后续执行将被抑制。

That being said, it would be nice if you posted the code inside SyncThread , otherwise it'll be nearly impossible to help you.话虽如此,如果您将代码发布在SyncThread中会很好,否则几乎不可能为您提供帮助。 Also, what's hilo ?另外,什么是hilo Why are you scheduling hilo to be executed, rather than carThread ?为什么要安排执行hilo而不是carThread

Try putting up a try-catch block in your Runnable implementation.尝试在 Runnable 实现中放置一个 try-catch 块。 Idea is to suppress any exceptions from being thrown out to the executor.想法是抑制任何异常被抛出给执行者。 Ideally, if you could log this incident in your catch block and avoid throwing it out, your application should work as expected, and you will be able to identify what happened afterwards.理想情况下,如果您可以在 catch 块中记录此事件并避免将其丢弃,那么您的应用程序应该可以按预期工作,并且您将能够确定之后发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM