Home Java JavaBase Learn the use of Java locks with Netty

Learn the use of Java locks with Netty

Jan 04, 2021 am 10:04 AM
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< 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!

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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Using Netty4 for TCP communication in Java API development Using Netty4 for TCP communication in Java API development Jun 17, 2023 pm 11:18 PM

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 Java development: How to use Netty for high-performance network programming Sep 20, 2023 pm 02:09 PM

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

What are the ways for springboot to integrate the netty framework? What are the ways for springboot to integrate the netty framework? May 10, 2023 pm 09:55 PM

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

PHP implements the open source Netty framework PHP implements the open source Netty framework Jun 18, 2023 pm 07:03 PM

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 How to use Java to develop a high-performance network application based on Netty Sep 20, 2023 pm 12:21 PM

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

Java back-end development: Use Netty to build a high-concurrency API server Java back-end development: Use Netty to build a high-concurrency API server Jun 17, 2023 am 10:23 AM

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 Java API development Using Netty for TCP communication in Java API development Jun 18, 2023 pm 11:34 PM

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

Overview of Java IO model and Netty framework Overview of Java IO model and Netty framework Apr 22, 2023 pm 02:34 PM

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

See all articles