Table of Contents Multithreading: Advantages of multithreading: Disadvantages of multithreading: Solution: Multi-thread synchronization Locking method one: synchronized() Inherit Thread() Implement the Runnable interface Locking method two: reentrantLock implements locking Multi-threading: In an application, there are multiple threads, and different threads can perform tasks in parallel. Multi-threading advantages: Improve program processing capabilities For example: anti-virus software […]
Tag: reentrantlock
ReentrantLock source code analysis
Concept: ReentrantLock is a reentrant lock that supports fair locks and unfair locks. The underlying implementation is based on AQS. Usage scenarios: It is more flexible and lightweight to use than Synchronized. ReentrantLock Synchronized Usage location Code block Method, code block Implementation mechanism AQS Monitor (monitor) Release form Explicit call, explicit release Automatic release Lock […]
ReentrantLock (reentrant lock)
Do you understand ReentrantLock? Is it a fair lock? ReentrantLock (reentrant lock) implements the Lock interface and is a reentrant and exclusive lock, and synchronized keyword is similar, but ReentrantLock is more flexible and powerful, adding advanced functions such as polling, timeout, interrupt, fair lock and unfair lock. Reentrancy means that in the same thread, […]
ReentrantLock lock and release lock source code analysis
ReentrantLock is a reentrant mutex lock, which means that it can only be held by one thread at the same point in time; reentrant means that the ReentrantLock lock can be acquired multiple times by the same thread. ReentrantLock has a distinction between fair and unfair. A FIFO waiting queue is used to manage the […]
AQS source code tracking in Reentrantlock (not finished)
AbstractQueuedSynchronizer (AQS), the subclass is Sync, and the subclass of Sync has two implementations: NonfairSync and FairSync. lock() .lock() of NofairSync object final void lock() {<!– –> //state0 represents not locked //1 represents locking if (compareAndSetState(0, 1)) //The first cas lock is successful setExclusiveOwnerThread(Thread.currentThread()); //Set the thread in the lock to the current thread else […]
“In-depth exploration of ReentrantLock lock in Java JUC: Implementing multi-thread synchronization and concurrency control”
Introduction 1. Starting from Java5, Java provides a more powerful thread synchronization mechanism – synchronization is achieved by explicitly defining a synchronization lock object. Under this mechanism, the synchronization lock is acted by a Lock object. 2. Lock provides a wider range of locking operations than synchronized methods and synchronized code blocks. Lock allows for […]
ReentrantLock (reentrant lock)
Reentrant locks implemented based on AQS include fair and unfair implementation methods. The difference between fair and unfair: 1. Unfair: Actively grab the lock, fail to grab the lock, enter the lock acquisition process implemented by AQS (queue) 2. Fair lock: Do not actively grab the lock and directly enter the AQS method of acquiring […]
7 ReentrantLock bottom layer
Table of Contents 1 tube 2 AQS principle analysis 2.1 Introduction to AQS 2.2 AQS core structure 2.3 AQS two queues 2.3.1 Synchronous waiting queue 2.3.2 Conditional waiting queue 3 ReentrantLock source code 3.1 Fair/Unfair 3.2 Reentrant lock 3.3 Overall process 3.3.1 Locking 3.3.2 Unlocking 1 pipe Two implementations of monitors in Java: sychronized: Object […]
Android—Synchronized and ReentrantLock
Basic use of Synchronized 1. Modify instance methods public class SynchronizedMethods{ private int sum = 0; public synchronized void calculate(){ sum = sum + 1; } } The lock object in this case is the current instance object, so only calling this method on the same instance object will have a mutual exclusion effect; there […]
ReentrantLock source code analysis
When we read the source code of ReentrantLock, we found the following three classes: Among them: There are three classes: Sync, NonfairSync, and FairSync inside the ReentrantLock class. The NonfairSync and FairSync classes inherit from the Sync class, and the Sync class inherits from the AbstractQueuedSynchronizer abstract class. Of course, the ReentrantLock class itself inherits […]