简体   繁体   English

Java无限制的线程在一段时间后停止

[英]Java unlimited thread stops after some time

I have a web server and some data storage kind of servers. 我有一个Web服务器和一些数据存储类型的服务器。 What I need is synchronization between both the servers. 我需要的是两台服务器之间的同步。 Web servers are always up, and I try to keep the data servers up as long as possible. Web服务器总是处于运行状态,我尽可能地保持数据服务器的运行。

Both have MySQL databases and I already have written scripts to synchronize them. 两者都有MySQL数据库,我已经编写了脚本来同步它们。 I have made a java program which gets the new data fields from the web server and stores them in its own database every 10 seconds. 我创建了一个java程序,它从Web服务器获取新数据字段,并每隔10秒将它们存储在自己的数据库中。 An unlimited java thread does this. 无限制的java线程就是这样做的。 This would be the piece of code which does this: 这将是执行此操作的代码段:

Timer timer;
Date current_date = new Date() ;
    timer = new Timer();
    timer.scheduleAtFixedRate(new doCheck(), current_date, 10 * 1000);

The constructor of the class doCheck() is fetching data from the web server in XML format. doCheck()类的构造函数以XML格式从Web服务器获取数据。 No mysql direct connection b/w the servers is made. 没有mysql直接连接b / w服务器。

The real problem: 真正的问题:

But when this script runs 24/7, after a certain period like in a couple of days, the script (basically a thread fetching XML from a web server) suddenly stops running and the synchronization totally disconnects. 但是当这个脚本全天候运行时,在几天之后的一段时间内,脚本(基本上是从Web服务器获取XML的线程)突然停止运行并且同步完全断开连接。

Possibility #1: Internet disconnection: I tried unplugging the network cable, and it started giving exceptions, but as soon as I reconnected the wire, it start working perfectly fine. 可能性#1:互联网断开:我尝试拔掉网络电缆,并开始提供例外,但一旦我重新连接电线,它就开始工作得非常好。

Possibility #2: Any exception in the code etc.: I am maintaining logs, but most of the times it stops suddenly without and kind of exception or error. 可能性#2:代码中的任何异常等:我正在维护日志,但大多数情况下它会突然停止而没有异常或错误。

Possibility #3: Manual cancellation of thread: No one touches the servers :P and there is just one button to cancel it, so this is not possible. 可能性#3:手动取消线程:没有人接触服务器:P并且只有一个按钮可以取消它,所以这是不可能的。

Why does this happen? 为什么会这样? I just want this script to run unlimited times. 我只想让这个脚本无限次运行。

I can't offer a concrete reason for this would be happening, but the Timer only has one internal thread and if this Thread dies then it does not get automatically restarted. 我不能提供一个具体的原因,这将发生,但Timer只有一个内部线程,如果这个线程死了,那么它不会自动重启。 So if you have an exception thrown out of the run() method of a TimerTask just once then the Timer is dead forever. 因此,如果您从TimerTask的run()方法抛出一个异常,那么Timer将永远消失。

You may be missing this exception so I would do the following 您可能会遗漏此异常,因此我会执行以下操作

  • Implement the default thread exception handler and make sure to log the result somewhere where you can find it 实现默认线程异常处理程序 ,并确保将结果记录在可以找到它的位置
  • Optionally implement a try/catch/rethrow block in your TimerTask implementation. (可选)在TimerTask实现中实现try / catch / rethrow块。 Don't swallow the exception here, just log it and rethrow it (for now, see below). 不要在这里吞下异常,只需记录并重新抛出它(现在,见下文)。

If there is an exception being thrown out of your TimerTask you should see it twice, once in the TimerTask try/catch and once by the default exception handler when the Timer thread dies. 如果从TimerTask中抛出异常,您应该看到它两次,一次在TimerTask try / catch中,一次在Timer线程死亡时由默认异常处理程序查看。 This will confirm that the Timer thread is being killed by the exception. 这将确认Timer线程被异常杀死。

Assuming that is the case then you'll have to look at the exception and decide how you want to handle it. 假设是这种情况,那么你将不得不查看异常并决定如何处理它。 You can catch that specific exception type and log/swallow it (modify your TimerTask implementation to suit), or maybe let the exception propogate and raise an alert in the system. 您可以捕获该特定异常类型并记录/吞下它(修改您的TimerTask实现以适应),或者让异常传播并在系统中引发警报。

It might be worth switching to use the ScheduledExecutorService introduced in 1.5 as it is bit more useful and MAY be more robust if the a scheduled task throws an exception (I'm not 100% on this, check the relevant API docs for the implementation). 可能值得切换到使用1.5中引入的ScheduledExecutorService ,因为它更有用,如果计划任务抛出异常,可能会更健壮(我不是100%,请查看实现的相关API文档) 。

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

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