简体   繁体   中英

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

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

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).

From the question, I understand that, you are talking about 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.

2) another thing is that you have to handle this scenarios so that exception cases are reduced. 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.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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