ExecutorService中timeout的理想值

huangapple go评论77阅读模式
英文:

Ideal value for timeout in ExecutorService

问题

我想了解关于 awaitTermination() 方法中理想超时值的情况,用于 ExecutorService。我们应该如何决定理想的超时时间?如果将超时时间设置为一天,是否会有问题?

boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;

假设我正在从一个数据库加载数据到另一个数据库。由于网络问题,有时可能会减慢速度,甚至要上传的文档数量也可能增加。因此,很难判断最长所需时间。在这种情况下,如果我将超时时间设置为一天(实际上,我希望从 ExecutorService 的角度使用没有超时的 awaitTermination()),这样做会有什么缺点?

awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS) 有什么不利之处?

因为无论如何,ExecutorService 会在任务完成后终止。

英文:

I want to understand about the ideal value of timeout in awaitTermination() method for executorservice. How should we decide the ideal timeout? Does it harm if we use one day as the timeout?

boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;

suppose I am loading data from one database to another database. Sometimes it may slow down because of network issues and even the number of documents to upload may also increase. So it becomes a little difficult to judge the max time. So in that scenario, if I use one day as a timeout (Actually, I want to use awaitTermination() without a timeout from executorService side). What exactly are the disadvantages of this?

What is the downside of awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS)?

because anyways, executorservice will terminate once the tasks are done

答案1

得分: 1

这取决于任务可能需要多长时间。

在最坏的情况下,你最长的任务需要多长时间?
假设在最坏的情况下需要1分钟。那么就取2分钟。
你只需要计算最坏情况的时间加上一个缓冲时间。

你只是不希望已经启动的作业无故被取消,因为这可能导致数据丢失。

如果你在一个带有确认机制的队列上工作,只有在作业完成后才确认,你可以立即终止,因为在下一次启动和超时后,你的工作者会再次获得该任务。(但要小心。如果你的服务器重新启动,可能会陷入循环。)

英文:

This depends on how long a task can consume.

How long in the worst case you longest task will take?
Lets say it will take in worst case 1 min. Then take 2 min.
You simply calculate worst case time plus a buffer.

You simply don't wont that the started job is canceled without reason because you can get a data loss.

If you are working e.g. on a queue with confirmation and you confirm only if your job is done, you can terminate immediately because on next start and after timeout your worker gets the task again. (But be carefully. If you servere restarts you can end up in a loop)

huangapple
  • 本文由 发表于 2020年9月4日 19:42:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63740499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定