In multithreading, prior to version 1.5, we used synchronized blocks of code or synchronized methods to solve thread safety problems
Such as:
Sync block
Synchronized (Object){
Functional code;
}
Synchronized methods
public synchronized void test(){
Functional code;
}
// The data is shared first
class Person{
String name;
boolean isMoney=true;
/ / to make money
public void zhengQian(){
synchronized(this){
while(! isMoney){
try{wait(); }catch(Exception e){}
}
Name = “man”;
System. Out. Println (www.sangpi.comThread.currentThread (). The getName () + name + “earn money — — — — — –“);
isMoney=! isMoney;
notifyAll(); // Wake up all
}
}
/ / to spend money
public void huaQian(){
synchronized(this){
while(isMoney){
try{wait(); }catch(Exception e){}
}
Name =” Women… People “;
System. Out. Println (Thread. CurrentThread (). The getName () + name + “flower – = = = = = = = = = money — -“);
isMoney=! isMoney;
notifyAll(); // Wake up all
}
}
}
// input thread
class In implements Runnable{
Person p=null;
In(Person p){
this.p=p;
}
public void run(){
while(true){
p.zhengQian();
}
}
}
// Output thread
class Out implements Runnable{
Person p=null;
Out(Person p){
this.p=p;
}
public void run(){
while(true){
p.huaQian();
}
}
}
As you can see from the code, it is the lock object this that controls the locking code for either a synchronized block or a synchronized method.
The API also tells us:
The Lock implementation provides a wider range of locking operations than are available using synchronized methods and statements. This implementation allows for a more flexible page-game structure, can have vastly different properties, and can support multiple related Condition objects.
How does this work with Lock+Condition?
The code is as follows:
import java.util.concurrent.locks.*;
class Person{
String name;
boolean isMoney=true;
Lock lock=new ReentrantLock();
Condition in=lock.newCondition();
Condition out=lock.newCondition();
/ / to make money
public void zhengQian(){
lock.lock();
try{
while(! isMoney){
try{in.wait(); }catch(Exception e){}
}
Name = “man”;
System. The out. Println (Thread. CurrentThread (). The getName () + name + “earn money — — — — — –“);
isMoney=! isMoney;
out.signal(); // Wake the person up
}finally{
lock.unlock();
}
}
/ / to spend money
public void huaQian(){
lock.lock();
try{
while(! isMoney){
try{out.wait(); }catch(Exception e){}
}
Name =” Women… People “;
System. Out. Println (Thread. CurrentThread (). The getName () + name + “flower – = = = = = = = = = money — -“);
isMoney=! isMoney;
in.signal(); // Wake the person up
}finally{
lock.unlock();
}
}
}
By comparison, it can be found that Lock+Condition uses Condition object to specifically operate the wait and wake in the field.
That is, the Condition object of the producing thread has its own Condition for the producing thread, and the Condition of the consuming thread has its own Condition for the consuming thread
Object, which has precise control over which thread is waiting or being woken up. The synchronization code block or method cannot be woken up precisely
Wake up the other party, so only all can be awakened, which increases the judgment times of the thread. Obviously, LOCK + CONDITION is superior to synchronized
Next, let’s analyze the relationship between Lock and Condition
The object subclass of Lock replaces synchronized, that is, the Lock, and the Condition is simply an operon of the Lock
In place of the previous lock objects, which have wait and wake methods, the Condition is primarily an object of action on the lock, despite the different names
A Lock object creates three Condition objects x,y, and z
Threads that execute the lock in the future will be divided into three types, such as those that execute this code if they execute x.await()
If all three red threads in z are in a wait state, executing z.signal() will wake up one of the three red threads executing z, and the code will run normally.
The two sides of the blue line represent two subclass objects of Lock respectively, and the conditions of each Lock can operate on each other, for two
Condition between locks, such as X and Q, is not directly related
OK, so this article is mainly about replacing synchronized with Lock+Condition. I hope it will be helpful.