java - What's the benefit of having two ReentrantLock in a class? -
if have code:
public class prlock {     private lock lock1= new reentrantlock();     private lock lock2= new reentrantlock();     private int num=0;      public void addlock1(){         lock1.lock();         try {             num++;             system.out.println(thread.currentthread().getname()+" num "+num);         } finally{             lock1.unlock();         }     }     public void addlock2() {         lock2.lock();         try {             num++;             system.out.println(thread.currentthread().getname()+" num "+num);         } finally{             lock2.unlock();         }     } }   what difference between lock1 , lock2, simple alias or there different logic?
if use 1 lock 2 methods won't able execute them concurrently.
so having 2 locks means, while executing addlock1() thread-a can execute addlock2() thread-b. thread-b blocked, if had 1 lock.
Comments
Post a Comment