How to understand the lock of Netty
How to understand Netty locks, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
1. Analyze the three core elements of synchronization problem.
Atomicity: "it can't be unassailable without one go", such as iTunes + under high concurrency.
Visibility: "the changes you make are invisible to others."
Order: "not playing cards according to the routine"
2. Classification of locks
Competitive attitude: optimistic lock (atomic class in java.util.concurrent package) and pessimistic lock (synchronized)
Whether the person waiting for the lock is fair: fair lock new ReentrantLock (true) and unfair lock new ReentrantLock ()
Whether it can be shared or not: shared lock and exclusive lock-ReadWriteLock, whose read lock is shared lock and its write lock is exclusive lock
3. Five key points for Netty to play with lock 3.1, the object and scope of lock are concerned-> reduce granularity
Example: initialize channel (io.netty.bootstrap.ServerBootstrap#init)
Synchronized method-> Synchronized block
Void init (Channel channel) throws Exception {Map, Object > attrs = this.attrs0 (); synchronized (attrs) {Iterator var5 = attrs.entrySet (). Iterator (); while (true) {if (! var5.hasNext ()) {break;} Entry