Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to understand the lock of Netty

Shulou Source: shulou.com Published: 2022-06-02 03:23:06 09月13日 Update

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

Tags: Service byte object different waiter mode box atom scene system problem up and down context no content function multiple local overhead overall Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Docker Shulou Technology macOS Huawei