This lib is built on google/guava based on Striped.
Main purpose of concurrent create clean quick and simple implementation of Striped lock.
Synchronize some execution block by some resource(key).
Lock lock = StripedLockFactory.createLock(StripedLockType.LOCK, 8, 30); // See the javadoc to params information
String key = "taskA";
lock.lock(key, () -> {
//the code which must be executed.
});
or if locked statement returns result
Lock lock = StripedLockFactory.createLock(StripedLockType.LOCK, 8, 30); // See the javadoc to params information
String key = "taskA";
Object lockResult = lock(key, () -> {
//the code which must be executed.
return result;
});
Note: You can throw your custom checked exception from above lambda block and handle it outside of lock method
Handel InterruptedException
If thread can be interrupted you must use the method lockInterruptibly
.
This method throws InterruptedException when current thread is interrupted.
Lock lock = StripedLockFactory.createLock(StripedLockType.LOCK, 8, 30); // See the javadoc to params information
String key = "taskA";
lock.lockInterruptibly(key, () -> {
//the code which must be executed.
});
Lock with various keys
When you have some keys collection to lock some block you can do following:
Lock lock = StripedLockFactory.createLock(StripedLockType.LOCK, 8, 30); // See the javadoc to params information
Collection<String> keys = ImmutableList.of("taskA", "taskB");
lock.lock(keys, () -> {
//the code which must be executed.
});
Create lazy weak lock instance
Lock lock = StripedLockFactory.createLock(StripedLockType.LAZY_WEAK_LOCK, 8, 30); // See the javadoc to params information
For difference between StripedLockType.LOCK and StripedLockType.LAZY_WEAK_LOCK you can see javadoc.
To use from maven add this snippet to the pom.xml dependencies
section:
<dependency>
<groupId>com.jsunsoft.util</groupId>
<artifactId>concurrent</artifactId>
<version>1.0.2</version>
</dependency>