search
HomeJavaJavaBaseLearn the use of Java locks with Netty

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

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!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.