Original public number: Java top student

In addition to the synchronized can solve the problem of atomic sex, Jdk1.5, in Java. Util. Concurrent. The locks. The Lock bag under the Lock can solve the problem of atomicity.

Under the Java. Util. Concurrent. The locks. The Lock has a set of implementation of thread synchronization interfaces and classes.

Lock is the interface, and we use its implementation class. There are many implementation classes for Lock, and we often use ReentrantLock.

PS: Students using IDEA can use the shortcut key [Ctrl+Alt+B] to view the implementation class of the interface when the interface class is selected.

For example, look at the Lock implementation class:

Let’s look at the source of the Lock interface:

public interface Lock {
    void lock();

    void lockInterruptibly() throws InterruptedException;

    boolean tryLock();

    boolean tryLock(long time, TimeUnit unit) throws InterruptedException;

    void unlock();

    Condition newCondition();
}

Copy the code

Method Explanation:

  • Void lock() is used to acquire the lock and, if it has already been acquired by another thread, wait until the lock is acquired

  • void lockInterruptibly() throws InterruptedException; If this method accepts a lock, it can respond to interruptions. For example, if you have two threads, one of which has already secured the lock and the other is waiting for the lock but does not want the Thread to wait for the lock, you can interrupt the Thread by calling thread.interrupted ()

  • boolean tryLock(); The tryLock method returns a bool that attempts to acquire the lock, true if it did, or false if it did not, but returns immediately instead of waiting forever

  • boolean tryLock(long time, TimeUnit unit) throws InterruptedException; This method is much the same as the tryLock method above, except that it will try for the specified time and return true if the lock has been obtained within the specified time, or false if the lock has not been obtained within the specified time

  • void unlock(); Release the lock

  • Condition newCondition(); Implement thread communication, equivalent to wait and notify, as explained separately later

To use a Lock, you need to manually release the Lock. However, if an exception is thrown in the program, the Lock cannot be released and may cause a deadlock.

So when we use Lock, we have a fixed format as follows:

Lock lock = new ReentrantLock(); l.lock(); Try {// business code............ } finally { l.unlock(); }Copy the code

I’m going to change the i++ example to use Lock to solve atomicity.

import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * and * all * * : 】 【 Java top students * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / public class AddILock {public static volatile int i = 0; static Lock lock = new ReentrantLock(); public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(() -> add(1000000)); Thread t2 = new Thread(() -> add(1000000)); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println(i); } public static void add(int n) { lock.lock(); try { for (int m = 0; m < n; m++) { i++; } } finally { lock.unlock(); }}}Copy the code

Execution Result:

2000000
Copy the code


Fair and Unfair Locks – How is ReentrantLock fair and unfair

A ReentrantLock is a ReentrantLock. The reentrant is known by its name. See this article: Reentrant -synchronized? How does ReentrantLock implement reentrant?

Tip: The IDEA shortcut key [Ctrl+Alt+U] can quickly view the class hierarchy diagram.

For example, look at the class diagram for NonfairSync:

Java memory model – Volatile – Three ways to use synchronized (example explained) Learn the principle of synchronization, update the article to understand Java thread pool reentrant lock – reentrant lock? How does ReentrantLock implement reentrant? Termination of a thread – What is the difference between stop and interrupt?

Original public number: Java top student