There are two ways to implement the lock mechanism in Java, one in the previous blog ("Java7 Concurrent programming Combat"-----thread synchronization mechanism: synchronized) introduced synchronized, And the other is more powerful and led lock than synchronized. Lock ensures that when one thread is in the critical section of the code, another thread does not enter the critical section, which provides a more powerful and flexible locking mechanism relative to the Synchronized,lock interface and its implementation classes.
A simple lock.
When using synchronized, we use locks in this way:
public class ThreadTest {public void Test () { synchronized (this) { //do something } }}
Synchronized can ensure that only one thread is executing dosomething at the same time. Here is the use of lock instead of synchronized:
public class ThreadTest { lock lock = new Lock (); public void Test () { lock.lock (); Do something lock.unlock (); }}
The lock () method locks the lock instance object so that all threads that call the lock () method on the object are blocked until the unlock () method of the lock object is called.
"The following citation: Locks in Java"
public class lock{ private Boolean isLocked = false; Public synchronized void Lock () throws interruptedexception{ while (isLocked) { wait (); } IsLocked = true; } public synchronized void Unlock () { isLocked = false; Notify (); }}
When IsLocked is true, the thread calling lock () blocks the wait on the wait () call. To prevent the thread from receiving the Notify () call and returning from Wait (), the thread will re-examine the islocked condition to determine whether it is currently safe to continue or if it needs to wait again, instead of thinking that the thread has been woken up and can safely continue execution. If isLocked is false, the current thread exits the while (isLocked) loop and sets isLocked back to true so that other threads that are calling the lock () method can lock on the lock instance.
The fairness of the lock
The opposite of fairness is hunger. So what is "hunger"? If a thread is not getting CPU uptime because other threads have been hogging the CPU, then we call the thread "starved to death". And the solution to hunger is called "fairness"-all threads have a fair chance of getting a CPU run.
There are several reasons that lead to thread starvation:
high-priority threads devour the CPU time of all low-priority threads . We can set its priority for each thread individually, from 1 to 10. The higher the priority, the more time the thread gets the CPU. For most applications, we'd better not change their priority values.
the thread is permanently blocked in a state waiting to enter the synchronization block . Java's synchronous code area is an important factor that causes thread starvation. Java's synchronous code block does not guarantee the order in which threads enter it. This means that theoretically one or more threads are stuck in an attempt to enter the synchronized code area, because other threads are always better than he gets access, causing it to get "starved to death" until the CPU runs out of chances.
the thread waits for an object that itself is also permanently waiting to be completed . If more than one thread is executing on the Wait () method, and calling notify () on it does not guarantee which thread will wake up, any thread may be in a state of continuing waiting. So there is a risk that a waiting thread never wakes up because the other waiting threads always get awakened.
To solve the thread "hunger" problem, we can use locks to achieve fairness.
Re-entry of the lock
We know that when a thread requests an object that holds a lock by another thread, the thread blocks, but can it succeed if the thread requests an object that holds the lock by itself? The answer can be successful, the guarantee of success is the "reentrant" of the thread lock.
Reentrant means that you can get your own internal lock again without blocking. As follows:
public class Father {public synchronized void method () { //do something }}public class child extends father{< C4/>public synchronized void Method () { //do something super.method ();} }
If it is not reentrant, the above code will deadlock, because calling the child's method (), first gets the parent class father's built-in lock and then obtains the child's built-in lock, when calling the method of the parent class, need to go back to the parent class's built-in lock, if not reentrant, may be caught in deadlock.
The implementation of Java Multi-threading reentrant is to correlate a request calculation with each lock and a thread that occupies it, when the count is 0 o'clock and the lock is considered not to be occupied, then any thread can acquire the lock's possession. When a thread requests success, the JVM logs the lock's holding thread and sets the count to 1, which must wait if another thread requests the lock. When the thread requests a request for a lock again, the count is +1, and when the owning thread exits the synchronization code block, the count is 1 until 0 o'clock, releasing the lock. At this point other threads have access to the lock's possession.
Lock and its implementation class
Java.util.concurrent.locks provides a very flexible locking mechanism that provides a framework interface and class for lock and wait conditions, unlike built-in synchronization and monitors, which allow for more flexibility in using locks and conditions. Its class structure diagram is as follows:
Reentrantlock: A reentrant mutex that is the primary implementation of the lock interface.
Reentrantreadwritelock:
Readwritelock: Readwritelock maintains a pair of related locks, one for read-only operations and the other for write operations.
Semaphore: a count semaphore.
Condition: The association condition of a lock, which is intended to allow a thread to acquire a lock and to see if one of the waiting conditions is satisfied.
Cyclicbarrier: A synchronization helper class that allows a group of threads to wait on each other until a common barrier point is reached.
These classes will be described in more detail later.
Resources:
1. Locks in Java
2. "Java Concurrency and multithreading" hunger and fairness
Java-"JUC" lock