[Community Library] "In-depth understanding of high concurrent programming: JDK core technology" after reading

About the Author

        The first thing to introduce is definitely the author of "In-depth Understanding of High Concurrency Programming: JDK Core Technology": Binghe , as a senior Internet technical expert, database technical expert, and distributed and micro-service architecture expert. He has been committed to the research of distributed system architecture, microservices, distributed database, distributed transaction and big data technology for many years, and has rich experience in the fields of high concurrency, high availability, high scalability, high maintenance and big data. Architecture development experience.

        Not only that, Binghe also has other works, such as "Visual Multi-Data Source Heterogeneous Middleware mykit-data", the prequel of the book "In-depth Understanding of High Concurrency Programming: Core Principles and Case Practices", "In-depth Understanding of Distributed Transactions: Principles and Actual Combat", "Massive Data Processing and Big Data Technology Actual Combat", "MySQL Technology Encyclopedia: Development, Optimization and Operation and Maintenance Actual Combat" and so on. If you still want to understand the author more deeply, here is the link to his blog on CSDN. Interested readers can go to have a deeper understanding of the author Glacier:

null Glacier is good at proficient in big data series, proficient in Java series, proficient in MySQL series, etc. Knowledge, Glacier focuses on architecture, neural network, redis, recommendation algorithm, tensorflow, elasticsearch, web security, spark, devops, security architecture, linux, jenkins , mysql, hadoop, storm, network, deep learning, automation, system architecture, sorting algorithm, flink, clustering, nlp, security, system security, centos, classification field. https://blog.csdn.net/l1028386804?type =lately

deep understanding

        "In-depth understanding of high-concurrency programming: JDK core technology" is a technical book that introduces high-concurrency programming. Through the analysis of JDK core technology, it deeply discusses the principles, methods and techniques of high-concurrency programming. It is a good book worth recommending. Book.

        The length of this book is not long, but the content is very practical, it is a good technical practice guide. The author starts with Java's multithreading, thread pool, concurrent collections, locks, etc., and through detailed code examples and interpretations, deeply analyzes the related technologies of high-concurrency programming and the practical application of these technologies.

        In this book, the author introduces the basic knowledge of Java multi-thread process designing in detail, so that readers can quickly master the basic concepts, methods and techniques of Java multi-thread process designing. At the same time, the author also introduces the implementation methods of multi-thread programming in various practical scenarios, and discusses the advantages, disadvantages and scope of application of these implementation methods.

        In addition, this book also explains in detail the core technologies of Java concurrent collections, thread pools, and locks, and solves some common problems in high-concurrency programming, such as thread safety and performance issues. The author also provides a variety of excellent solutions and best practices, so that readers can understand and apply these technologies more deeply, so as to achieve high-concurrency programming more efficiently.

        In short, this book introduces the core technology of high-concurrency programming in detail, which is practical and instructive. Through a large number of practical cases and code examples, readers can deeply understand the principles and methods of high-concurrency programming. Whether you are a Java beginner or a developer with certain programming experience, this book is a technical book worthy of in-depth reading.

Structure of this book

        Starting from the actual needs, the book divides the whole book into three major chapters, namely: the basic knowledge of JDK high-concurrency programming, core tools and core technology of thread pool. Look at the picture below:

insert image description here

The first basic article (chapters 1~2)

        This article briefly introduces the basic concepts of processes and threads, thread scheduling and context switching, a comprehensive comparison of processes and threads, how to view the runtime information of processes and threads, and the basic operations of threads and thread groups.

Process
        To some extent, a process is actually a running program instance in the memory, which is allocated a certain space, and each process does not interfere with each other during the running process.
The basic concept and composition of a process
        Generally speaking, a process is an instance running in memory, and it is the basic unit for system resource allocation. Currently, most programs can start and run multiple process instances at the same time, and some programs can only start and run one process instance. For example, when a browser is opened, multiple process instances are started; when a Java program is run, a corresponding JVM process instance is started. Processes are dynamic, concurrent, independent and asynchronous. ·The following concepts of process are also learned:

  • Process ID: The unique number of the process.
  • Process mutual exclusion: When multiple processes operate the same resource, only one process is allowed to use the resource at the same time, and other processes must wait when the current process uses the resource until the current process releases the resource. Critical resources: resources that only one process is allowed to access at the same time.
  • Critical section: A critical section is essentially a code fragment, that is, a code fragment that accesses critical resources in a process, and it is necessary to ensure that the processes enter their respective critical sections exclusively.
  • Process synchronization: A process in which multiple concurrently running processes are executed in a certain order.

        And let me learn: thread is the basic unit of CPU scheduling, using multi-threaded programming can make better use of CPU resources and give full play to the computing performance of multi-core CPUs. In Java's multi-threaded programming, there are many simple and easy-to-use APIs, which can be used to easily create threads and operate on threads. Thread creation methods mainly include inheriting the Thread class, implementing the Runnable interface, implementing the Callable interface, using FutureTask with Thread, and using a thread pool.

The second core tools (Chapter 3~13)

        This article introduces various concurrency tools of JDK in detail through a large number of source codes and cases, covering synchronous collections, concurrent List collection classes, concurrent Set collection classes, concurrent Map collection classes, concurrent blocking queues, concurrent non-blocking queues, concurrent tool classes, locks Tool classes, lock-free atomic classes, thread tool classes, and asynchronous programming tool classes. Almost every chapter is equipped with the source code and practical cases of JDK core tool classes, which is helpful for readers to understand.

        Let me know that JDK provides a very rich collection class, concurrency tool class, lock tool class, thread tool class, Java8 has added tools for parallel stream operations and asynchronous programming, etc., using the concurrent programming provided by JDK Tool classes can greatly reduce the complexity of concurrent programming.

Synchronization Collection Vector

        For example, the synchronized collection Vector: Vector is a thread-safe synchronized collection class provided since JDK1.0. In some cases, it is necessary to protect the compounding in the Vector collection by locking additional clients or synchronized collection classes. operate. When performing compound operations such as cyclic traversal on the Vector collection, obtaining the next element according to the current element, or performing follow-up logic according to a certain condition, if other threads concurrently modify the elements in the Vector collection, thread safety issues will arise.

Concurrent collection CopyOnWriteArrayList

        There are also concurrent collections: only the CopyOnWriteArrayList copy technology is used in the concurrent List collection in the Java concurrent package. Adding, modifying or deleting data in CopyOnWriteArrayList is based on one of the underlying arrays. The CopyOnWriteArrayList class is a thread-safe class. Each CopyOnWriteArrayList class has a member variable array of the object array class used to point to the array of storage elements, and a member variable lock of the ReentrantLock exclusive lock type to ensure that one thread can modify the array array at the same time The data. The CopyOnWriteArrayList class inherits the CopyOnWriteArrayList class and implements the List interface. The front layer of CopyOnWriteArrayList is essentially an array. The read operation of this array will directly return the value in the original array: the write operation of this array (creation of adding elements, modifying elements and deleting elements) will first obtain the ReentrantLock exclusive lock, and then copy a copy of the underlying shadow array, in the array array The write operation is performed on the copy, and after the execution is completed, the copy of the array array is assigned to the reference.

The third thread pool core technology article (chapters 14~16)

        This article deeply analyzes the core source code of the thread pool in JDK. Including the top-level interface and abstract class of the thread pool, the core process of the thread pool running correctly, the core process of the thread pool executing tasks, the core process of the Worker thread, the core process of the thread pool graceful exit, the difference between the ScheduledThreadPoolExecutor class and the Timer class, and the timing task thread Pool initialization, scheduling process, graceful shutdown process, etc. Through the study of this article, readers can deeply understand the core principle and execution process of the thread pool from the source code level.

        In a real high-concurrency scenario, the Thread class is generally not used to create threads directly, but the thread pool is used to create and manage threads. It can be said that learning the thread pool well is very important for concurrent programming.

Introduction to thread pool

        The creation and recycling of the thread pool is a process that consumes a lot of system resources. If threads are created and recycled frequently in the system, the execution performance of the program will be greatly reduced. Moreover, creating a large number of threads in a short period of time may cause problems such as 100% CPU usage, crash, or memory overflow. Using a thread pool can solve these problems very easily.

Thread pool core class inheritance relationship

        The thread pool is a thread usage mode provided by Java since JDK 1.5, which can automatically create and recycle threads, and manage the life cycle of threads. Multiple threads can be managed and maintained in the thread pool.
        Java's thread pool is mainly implemented through the Executor framework, involving Executor interface, ExecutorServcie interface, AbstractExecutorService abstract class, ScheduledExecutorService interface, ThreadPoolExecutor class and ScheduledThreadPoolExccutor class. The core class to implement the thread pool is ThreadPoolExecutor, and the ScheduledThreadPoolExecutor class implements the scheduled task function, which can make the tasks submitted to the thread pool be executed regularly and regularly. In order to facilitate the creation of thread pools, JDK also provides an Executors tool class. The Executors class encapsulates the name methods for creating thread pools, which are specially used to create thread pools. However, in a real high-concurrency scenario, it is not recommended to use the Execulors tool class to create a thread pool, but to directly use the ThreadPoolExecutor class to create a thread pool.

 

Guess you like

Origin blog.csdn.net/qq_53317005/article/details/130935176