Example
Add a lock to segmentation_object this string
objectlock Objectsegmentationlock = new Objectlock ("Segmentation_object");
try {
objectsegmentationlock.lock ();
DoSomething ();
} catch (Exception e) {
e.printstacktrace ();
} finally {
objectsegmentationlock.releaselock ();
}
try {
//attempt to lock up until 5 seconds to timeout lock failure
if (Objectsegmentationlock.trylock (5000)) {
dosomething ()}
}
catch (Exception e) {
e.printstacktrace ();
} finally {
objectsegmentationlock.releaselock ();
}
Codelock Codelock = new Codelock ();
try {
//directly block and lock the following code (dosomething) until the lock
Codelock.lock () is obtained;
DoSomething ();
} catch (Exception e) {
e.printstacktrace ();
} finally {
codelock.releaselock ();
}
try {
//directly attempt to lock the following code (dosomething) until 5 seconds after timeout lock failure
if (Codelock.trylock (5000)) {
dosomething ();
}
} catch (Exception e) {
e.printstacktrace ();
} finally {
codelock.releaselock ();
}
Source Code
Https://github.com/lichao4Java/distributed-lock