Learn the use of Java locks with Netty
java basic tutorialThe column introduces how to learn Java lock 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!
3 Lock speed
Improve concurrencyLongCounter
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
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 itIn Netty application scenarios: partial serial and overall parallel>--one queue and multiple threads mode:
Avoid using locks: Use ThreadLocal to avoid resource contention, such as Netty lightweight Level thread pool implementation
Reduce the difficulty of user development, simplify logic, and improve processing performance
Avoid the additional overhead of context switching and concurrency protection caused by locksThe 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Java development: How to use Netty for high-performance network programming Summary: Netty is a high-performance, asynchronous event-driven network programming framework that simplifies the development process of network applications. This article will introduce the main features of Netty and how to use Netty for high-performance network programming. At the same time, we will also provide some specific Java code examples to help readers better understand and apply Netty. 1. Introduction to Netty Netty is a network programming box based on JavaNIO

TCP is a type of computer network communication protocol and a connection-oriented transmission protocol. In Java application development, TCP communication is widely used in various scenarios, such as data transmission between client and server, real-time transmission of audio and video, etc. Netty4 is a high-performance, highly scalable, and high-performance network programming framework that can optimize the data exchange process between the server and the client to make it more efficient and reliable. The specific implementation steps of using Netty4 for TCP communication are as follows: Introduction

With the continuous development of Internet technology, network programming is becoming more and more important. In this field, Netty is a very well-known framework. It is a high-performance, asynchronous event-driven network application framework that is widely used to develop various high-concurrency network applications. Netty is a Java framework, and its emergence has promoted the development of Java network programming. However, with the widespread use of PHP, PHP developers are also looking for frameworks capable of high-concurrency network programming. Therefore, this article describes how to exploit P

As a high-performance io framework, netty is a very easy-to-use technical framework. Netty is a client and server-side programming framework based on NIO. Using Netty can ensure that you can quickly and easily develop a network application, such as implementing some kind of Client and server applications of the protocol. Netty is equivalent to simplifying and streamlining the programming and development process of network applications, such as: socket service development based on TCP and UDP. "Fast" and "simple" need not create maintenance or performance problems. Netty is a project that absorbs the implementation experience of multiple protocols (including various binary text protocols such as FTP, SMTP, HTTP, etc.) and is quite carefully designed. In the end, Netty succeeded

With the continuous development of the Internet and the expansion of application fields, high concurrency has become an issue that must be considered in network application development. As a language widely used in enterprise-level application development, Java’s performance in high-concurrency application scenarios Much attention. Netty is a high-performance, asynchronous event-driven network application framework that has been widely used in the field of Java back-end development in recent years. This article will introduce the basic concepts and usage of Netty, and take building a high-concurrency API server as an example to demonstrate Netty

How to use Java to develop a high-performance network application based on Netty. Netty is a network programming framework based on JavaNIO technology and is widely used in high-performance network application development. In this article, we will explore how to use Java and Netty to develop a high-performance network application based on Netty. We will introduce the basic concepts and features of Netty and provide some code examples to help you better understand and use Netty. 1. Basic concepts and features of Netty Ne

In JavaAPI development, TCP communication is a very important component, and Netty5 is a set of high-performance network communication framework based on NIO, which can handle complex network communication tasks very conveniently. This article will introduce how to use Netty5 for TCP communication, including the core components of Netty5, an introduction to common APIs, and practical application cases. At the same time, this article will also introduce how to use Netty5 to improve the performance and reliability of TCP communication. 1. Netty5 core component of Netty5

In the Go language, Beego is a very popular web framework that can help us quickly build high-performance web applications. However, in some cases, we need more advanced network programming capabilities, such as handling high-concurrency network connections and real-time data transmission. Netty is a high-performance network programming framework that is widely praised in the Java community. This article will introduce how to use Netty in Beego for high-performance network programming. 1. Why choose Netty? When choosing a network programming framework,
