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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment