Java Executor Best Practices for Tasks that Should Run Forever

后端 未结 4 1232
不知归路
不知归路 2020-12-22 19:20

I\'m working on a Java project where I need to have multiple tasks running asynchronously. I\'m led to believe Executor is the best way for me to do this, so I\'m familiari

4条回答
  •  一个人的身影
    2020-12-22 19:25

    Aside to eyeballing it, I generally run Java code against static analysis tools like PMD and FindBugs to look for deeper issues.

    Specifically for this code FindBugs didn't like that results1 and results2 are not volatile in the lazy init, and that the run() methods might ignore the Exception because they aren't explicitly being handled.

    In general I am a bit leery of the use of Thread.sleep for concurrency testing, preferring timers or terminating states/conditions. Callable might be useful in returning something in the event of a disruption that throws an exception if unable to compute a result.

    For some best practices and more food for thought, check out Concurrency in Practice.

提交回复
热议问题