简体   繁体   English

异常后的线程处理

[英]Thread Handling after exception

used thread.sleep when exception occurs in my application if any thing exception occured in any one of the thread it should wait for some time and have to try again 当我的应用程序中发生异常时,如果在任何一个线程中发生任何事物异常,则应使用thread.sleep ,它应等待一段时间并必须重试

what i am facing the issue means for 100 request if exception occurs for 50 means that should be wait for some time and after try again if any exception occurs means it should also wait till success 我面临的问题是如果发生50次异常意味着100次请求意味着应该等待一段时间,如果发生任何异常再次尝试后意味着它也应该等待直到成功

am feeling this going to affect the performance and system usage. 感觉这会影响性能和系统使用率。

Please let me know your suggestions. 请让我知道您的建议。 Is it better to create new thread if any exception occurs? 如果发生任何异常,创建新线程是否更好? Or Any other way? 还是其他方式?

Retrying on exception isn't a bad idea, but you should make sure that: 重试异常不是一个坏主意,但是您应该确保:

  1. The exception is transient (ie you have reasons to believe it may succeed on retry, like a network error) 例外是暂时的(即,您有理由相信它可以重试成功,例如网络错误)
  2. You have ways to prevent retrying forever (like "max retry") 您可以采用多种方法来防止永远重试(例如“最大重试次数”)
  3. You don't overload the system by too frequent retries by eg using exponential back-off strategy (or even just a reasonably long wait time) 您不会因过于频繁的重试而使系统过载,例如使用指数退避策略(甚至只是相当长的等待时间)

Exception to rule 2 is possible (sometimes you do want stuff to retry forever). 规则2可能有例外(有时您确实希望永久重试内容)。

From the question, I understand that, you are talking about RunTimeExceptions . 从这个问题来看,我知道您在谈论RunTimeExceptions

1) Firstly I would like to suggest that, you should examine the code areas which may really throw the exceptions at run time ie processing of files. 1)首先,我想建议您检查在运行时可能真正引发异常的代码区域,即文件处理。

2) another thing is that you have to handle this scenarios so that exception cases are reduced. 2)另一件事是您必须处理这种情况,以便减少异常情况。 ie first check file is available then only try to read it, check permissions before writing it etc. In this way frequency of retrying may be reduced. 即第一个检查文件可用,然后仅尝试读取它,然后在写入之前检查权限等。这样可以减少重试的频率。 3) If both above actions fails, means the situation which can never be handled such as services are down etc., you can retry the considering all necessary aspects like - MAX try, MAX time out foe which you are re-trying, leave the attempt after certain amount of try OR time etc. 3)如果上述两项操作均失败,则意味着永远无法处理的情况(例如服务中断等),您可以重试考虑所有必要的方面,例如-MAX重试,MAX重试超时,然后重新尝试经过一定的尝试OR时间等尝试

Hope this helps. 希望这可以帮助。

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

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