io.funtom.util.concurrent.ReadWriteSynchronizedExecutor Maven / Gradle / Ivy
Show all versions of java-utils Show documentation
package io.funtom.util.concurrent;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Supplier;
/**
* An Executor which executes tasks on the caller thread.
*
* Calls to readExecute(...) methods:
*
- Never lock each other
* - Have the same memory semantics as locking and unlocking the read lock of a java.util.concurrent.lock.{@link ReadWriteLock}
*
* Calls to writeExecute(...) methods:
*
- Never overlaps with any other calls to execute methods
* - Have the same memory semantics as locking and unlocking the write lock of a java.util.concurrent.lock.{@link ReadWriteLock}
*
* Calling threads might be suspended.
*/
public final class ReadWriteSynchronizedExecutor {
private final SynchronizedExecutor readExecutor;
private final SynchronizedExecutor writeExecutor;
public ReadWriteSynchronizedExecutor() {
ReadWriteLock lock = new ReentrantReadWriteLock();
readExecutor = new SynchronizedExecutor(lock.readLock());
writeExecutor = new SynchronizedExecutor(lock.writeLock());
}
public void readExecute(Runnable task) {
readExecutor.execute(task);
}
public R readExecute(Supplier task) {
return readExecutor.execute(task);
}
public void writeExecute(Runnable task) {
writeExecutor.execute(task);
}
public R writeExecute(Supplier task) {
return writeExecutor.execute(task);
}
}