14 | Lock and Condition (Part 1): Concealed hidden in concurrent packages
In the field of concurrent programming, there are two core issues: one is mutual exclusion , that is , only one thread is allowed to access shared resources at the same time; the other is synchronization , that is, how threads communicate and cooperate with each other. These two major problems can be solved by the management process. The Java SDK concurrent package implements management through two interfaces, Lock and Condition, where Lock is used to solve the mutual exclusion problem and Condition is used to solve the synchronization problem.
Reasons for reengineering
These three programs can make up for the problem of synchronized. I believe that you should be able to understand here. These three solutions are the main reason for "re-creating wheels", which are reflected in the API, which are the three methods of the Lock interface. Details are as follows:
// 支持中断的API
void lockInterruptibly() throws InterruptedException;
// 支持超时的API
boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
// 支持非阻塞获取锁的API
boolean tryLock();
How to ensure visibility
It uses the Happens-Before rule related to volatile
What is a reentrant lock
Fair lock and unfair lock
Best practices for using locks
Always lock only when updating member variables of an object.
Always lock only when accessing variable member variables.
Never lock when calling methods of other objects
15 | Lock and Condition (Part 2): How does Dubbo use the pipe process to achieve asynchronous transfer synchronization?
So how to use two condition variables to quickly implement a blocking queue?
Synchronous and asynchronous
Dubbo source code analysis
16 | Semaphore: How to quickly implement a current limiter?
Semaphore model
How to use semaphores
Quickly implement a current limiter
Semaphore can allow multiple threads to access a critical section.
Is there such a demand in reality? some. The more common requirements are the various pooled resources we encounter in our work , such as connection pools, object pools, thread pools, and so on. Among them, you may be most familiar with the database connection pool. At the same time, you must allow multiple threads to use the connection pool at the same time. Of course, each connection is not allowed to use other threads before it is released.
17 | ReadWriteLock: How to quickly implement a complete cache?
What is a read-write lock?
Quickly implement a cache
Implement on-demand loading of cache
Upgrade and downgrade of read-write lock
18 | StampedLock: Is there a faster lock than a read-write lock?
Three lock modes supported by StampedLock
ReadWriteLock supports two modes: one is read lock and the other is write lock. StampedLock supports three modes, namely: write lock, pessimistic read lock and optimistic read.
Further understanding of optimistic reading
Notes on using StampedLock
19 | CountDownLatch and CyclicBarrier: How to make multi-threaded pace consistent?
Use parallel optimization reconciliation system
Using CountDownLatch to achieve thread waiting
Further optimize performance
Using CyclicBarrier to realize thread synchronization
20 | Concurrent containers: What "pits" do we need to fill?
Synchronization container and its considerations
List、Map、Set和Queue
Concurrent containers and their considerations
(1) List
(2) Map
(3) Set
(4) Queue
1.单端阻塞队列 2.双端阻塞队列 3.单端非阻塞队列 4.双端非阻塞队列
21 | Atomic: A model of lock-free tools
Implementation principle of lock-free scheme
Atomic overview
- Atomic data types
- Atomic object reference type
- Atomic array
- Atomic object attribute updater
- Atomized accumulator
22 | Executor and thread pool: how to create the correct thread pool?
Thread pool is a producer-consumer model
How to use thread pool in Java
What to pay attention to when using thread pool
23 | Future: How to use multi-threading to achieve the best "boil tea" program?
How to get the task execution result
Realize the optimal "burning water and making tea" program
24 | CompletableFuture: Asynchronous programming is not that hard
CompletableFuture's core advantages
Create CompletableFuture object
How to understand the CompletionStage interface
How the CompletionStage interface describes serial relationships, AND aggregation relationships, OR aggregation relationships, and exception handling.
- Describe serial relationships
- Describe AND convergence
- Describe OR convergence
- Exception handling
25 | CompletionService: How to perform asynchronous tasks in batches?
Using CompletionService to Realize Inquiry System
So how do you create a CompletionService?
CompletionService interface description
Using CompletionService to realize Forking Cluster in Dubbo
26 | Fork / Join: Standalone version of MapReduce
For simple parallel tasks, you can use the "thread pool + Future" solution; if there is an aggregation relationship between tasks, whether it is an AND or OR aggregation, you can solve it through CompletableFuture; Can be solved by CompletionService.
Divide and conquer task model
Use of Fork / Join
How ForkJoinPool works
Simulate MapReduce to count the number of words
27 | Answers to hot issues of concurrent tool modules
Thoughts are left at the end of each article