use synchronized keyword
Object monitorObject;
synchronized(monitorObject){
//critical section
}
new way: use Lock.lock() and Lock.unlock()
Lock lockObject;
try{
lockObject.lock();
}
finally{
lockObject.unlock();
}
----
old way: use wait() and notify() in the critical section
//insdie your critical section
boolean somecondition;
while(somecondition){
wait();
}
boolean someOtherCondition;
if(someOtherCondition){
notify();
}
new way: use await() and signal() on condition variables
Condition conditionVariable = lockObject.newCondition();
boolean somecondition;
while(somecondition){
conitionVariable.await();
}
boolean someothercondition;
if(someOtherCondition){
conditionVariable.signal();
}
No comments:
Post a Comment