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

AI Hentai Generator
Generate AI Hentai for free.

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



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

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

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 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

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

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

Using Netty for TCP communication in JavaAPI development In modern software development, network communication has become an essential part. Netty is a Java framework for efficient and rapid development in high-performance network applications. It provides an easy-to-use API encapsulating JavaNIO and other network libraries. In JavaAPI development, Netty’s superiority can be reflected in the following aspects: High-performance Netty applications

What is Netty asynchronous? It is an event-driven network application framework that is used to quickly develop high-performance, highly reliable network IO programs. It is mainly aimed at high-concurrency applications for Clients under the TCP protocol. It is essentially a NIO framework, suitable for server communication, etc. Scenario asynchronous: sending a request without waiting for a response, the program continues on. Event-driven: a connection event or disconnection event, or a read event or a write event, and subsequent processing after the event occurs. Typical applications of Netty: high-performance rpc framework is used for remote service (process) calls, such as Dubbo. Game industry, page data interaction. Big data areas such as Hadoop high-performance communication and serialization components (AVRO). A simple understanding of the IO model is to use what pass
