Avoid over-synchronization
In order to avoid active failure and security failures, a synchronized method or code is fast, never give up control of the client.
Because it's alien, it's not controlled.
Move the foreign method call out of the synchronized code fast.
Create a Snapshot
Use concurrent collections, copyonwritearaylist. A Variant that belongs to ArrayList, where all writes are implemented by copying the entire underlying array. Because the internal data is used for no change, the iterations do not require locking and are very fast. However, if used extensively, performance will be greatly affected.
Foreign methods that are called outside the synchronization area are called open calls.
In addition to avoiding thinking, open calls can also greatly increase concurrency. Foreign methods may run for any length of time. If a foreign method is called within the synchronization region, other threads are denied access to the protected resource unnecessarily.
If a mutable class is to be used concurrently, this class should be made thread-safe, with internal synchronization, and you can gain significantly higher concurrency than locking the entire object externally.
StringBuffer instead of StringBuilder.
If you synchronize classes internally, you can use different methods to achieve high concurrency, such as split-lock, Split-lock, non-blocking concurrency control.
If a method modifies a static domain, access to the domain must be synchronized, even if it is often used only for a single thread.
Effective Java tenth concurrency avoid excessive synchronization of reading notes