synchronization - Can one explicitly lock/unlock implicit synchronized lock on an object in java -


yes synchronized locking synmtax easy because of it's block structure can't use it. there way explicitly lock , unlock "synchronized" monitor on object compatible prior usages of "synchronized" ?

class myobj {     object locker_ = new object();     void lockedmethod() {        synchronized(locker_) {            ....        }     }     iterator locktraversal() {         explicitlywaitforandacquirelock(locker_); // assume not throw exception         return(getaniterator());     }     void unlocktraversal() {         explicitlyreleaselock(locker_);     } }    myobj obj = (...)   try {      iterator = obj.locktraversal();      for(;;) // iterate   } {      obj.unlocktraversal();  } 

of course in example "closures" eliminate need, "stack scoped" destructors ;^> ...

you can't on object's intrinsic lock, can use java.util.concurrent.locks.lock instead.

if need it, feeling risky, , aren't worried portability, can use misc.sun.unsafe, monitorenter , monitorexit. but... not idea.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -