




























Working with shared mutable state is a huge burden that requires us to
endure added complexity and the accompanied greater chances of error.
When refactoring code, look out for some common concurrency-related
mistakes:
• Don’t create threads from within constructors; instead, create them in static factory methods
• Don’t create arbitrary threads; instead, use a pool of threads to reduce the tasks startup time and resource use.
• Ensure that access to mutable fields cross memory barrier and are visible to threads properly.
•Evaluate the granularity of locks and promote concurrency. Ensure that the locks are not overly conservative but are at the right level to provide adequate thread safety and concurrency at the same time.
• When working with multiple mutable fields, verify that the access to these variables is atomic; that is, other threads don’t see partial changes to these fields.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。