Multithreading is an advanced choice in learning the Java language. I recently learned a new skill, the ReentrantLock class, which is by far the easiest thread-safe way to use it, with the exception of violent synchronized.
Let’s start with a review of previous articles on thread-safe common synchronization classes:
- The CountDownLatch class is used in performance tests
- The CyclicBarrier class is applied in performance tests
- Phaser classes are applied in performance tests
- Java thread synchronizes three musketeers
The ReentrantLock class, named ReentrantLock, is similar to synchronized. I understand that this is a simple and easy to use thread-safe operation class, does not need to understand the syntax brought by synchronized, does not need other threads synchronization class lock memory of various API functions. ReentrantLock is a really useful multithreaded safety utility class.
The ReentrantLock core has two methods lock() and unlock(). As the name implies, one locks and one releases the lock. All thread-safe operations can be written between these two methods. This point with the previous article how to mock, moco fixed QPS fixed QPS interface upgrade compensation mechanism used in flow control class of thread-safe Java. Util. Concurrent. The use of Semaphore, Compared with Java. Util. Concurrent. The locks. Already equivalent to new Java. Util. Concurrent. The Semaphore ((1, true). The java.util.Concurrent package is a treasure trove that anyone interested in learning is welcome to browse the source code for fun.
ReentrantLock is an asynchronous lock that can be executed by a single thread in a single block of code. ReentrantLock is an asynchronous lock that can be executed by a single thread in a single block of code.
public static void main(String[] args) {
ReentrantLock lock = new ReentrantLock();
fun(() -> {
lock.lock();
output("The FunTester got the lock");
sleep(2.0);
lock.unlock();
output("FunTester released the lock.");
return null;
});
sleep(1.0);
lock.lock();
output("Main thread acquires lock");
lock.unlock();
output("Main thread releases lock");
}
Copy the code
The console output is as follows:
INFO->Main current user: oker, working directory: / Users/oker/IdeaProjects funtester/system coding format: utf-8, Mac OS X system version: 10.16
INFO->Main daemon thread :FT -d enabled!
INFO->Ft-1 FunTester got the lock
INFO->Ft-1 FunTester released the lock
INFO->Main The main thread acquires the lock
INFO->Main The main thread releases the lock
WARN->Ft-d asynchronous thread pool closed!
Process finished with exit code 0
Copy the code
The only thing to be aware of is that the UNLOCK () method may not be executed because of a program exception, so it is usually placed ina finally block.
Here are a few very useful apis to help address very common requirements:
Obtain the lock, but there is a timeout period, will return whether the lock was successful.
public boolean tryLock(long timeout, TimeUnit unit)
throws InterruptedException {
return sync.tryAcquireNanos(1, unit.toNanos(timeout));
}
Copy the code
Get the length of the wait queue. This is incorrect because the queue may change:
public final int getQueueLength(a) {
return sync.getQueueLength();
}
Copy the code
TryLocal = tryLocal = tryLocal = tryLocal = tryLocal
public boolean isLocked(a) {
return sync.isLocked();
}
Copy the code
ReentrantLock is used to obtain the number of times a thread has held a lock. If the number of times a thread has held a lock is not zero, no other thread can hold the lock successfully.
public int getHoldCount(a) {
return sync.getHoldCount();
}
Copy the code
Have Fun ~ Tester!
- FunTester test framework architecture diagram
- JMeter Chinese Operation Manual with a sense of time
- 140 interview questions (UI, Linux, MySQL, API, security)
- Graphic HTTP brain map
- Share a Fiddler study bag
- JVM command brain map for testing
- The Definitive Guide to Java Performance
- Java thread synchronizes three musketeers
- How do I choose the right test automation tool
- Handle JMeter variables with Groovy
- HTTP Interface Testing Basics
- Functional and non-functional testing
- How do I maintain automated tests
- Jira API to step on the pit