JDK中并发状态管理注意事项

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.

你可能感兴趣的:(jdk)