Home > Java > JavaBase > body text

Learn the use of Java locks with Netty

coldplay.xixi
Release: 2021-01-04 10:04:24
forward
2072 people have browsed it

java basic tutorialThe column introduces how to learn Java lock with Netty

Learn the use of Java locks with Netty

##Recommended (free): java basic tutorial

1 Lock objects and scope

    Reduce the granularity


2 The size of the lock object itself

    Reduce space occupation

  • Atomic long V.S long
    The former is an object, including an object header to save hashcode, lock and other information. It occupies 16 bytes under a 64-bit system. Under a 64-bit system:
  • volatile long= 8 bytes
  • AtomicLong = 8 bytes (volatile long) 16bytes (object header) 8 bytes (reference) = 32 bytes
  • Save at least 24 bytes!
So Atomic* objects =》 Volatile primary type Static Atomic*FieldUpdater

3 Lock speed

Improve concurrency

LongCounter

Used for functions such as recording the number of bytes of memory allocation.

Under high concurrency: java.util.concurrent.atomic.AtomicLong => java.util.concurrent.atomic.LongAdder

Conclusion: Measure and use the latest JDK features in a timely manner

According to different situations, choose different concurrent package implementations

JDK< 1.8 Consider ConcurrentHashMapV8 (the version of ConcurrentHashMap in jDK8)


4 Choose different concurrency classes for different scenarios

Change according to needs

  • Close and wait for the shutdown event executor (Event Executor) :

    Object.wait/ notify =》 CountDownLatch

  • Queue responsible for storing tasks in the Nio Event loop

    Jdk's LinkedBlockingQueue (MPMC, multi-producer Multiple consumers) -> jctools' MPSC
    io.netty.util.internal.PlatformDependent.Mpsc#newMpscQueue(int):


    5 The value of lock

    If you can’t use it, don’t use it

    In Netty application scenarios: partial serial and overall parallel>--one queue and multiple threads mode:
    Reduce the difficulty of user development, simplify logic, and improve processing performance
    Avoid the additional overhead of context switching and concurrency protection caused by locks

    Avoid using locks: Use ThreadLocal to avoid resource contention, such as Netty lightweight Level thread pool implementation


    The above is the detailed content of Learn the use of Java locks with Netty. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!