![JAR search and dependency download from the Maven repository](/logo.png)
io.funtom.util.concurrent.SynchronizedExecutor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-utils Show documentation
Show all versions of java-utils Show documentation
A Java utils library, contains common utils
package io.funtom.util.concurrent;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;
/**
* An Executor which executes tasks on the caller thread.
* The tasks will be executed synchronously, so no overlapping between two tasks running on different threads will ever occur.
* Calling threads might be suspended.
* Executing a task has the same memory semantics as locking and releasing a java.util.concurrent.locks.{@link Lock}.
*/
public final class SynchronizedExecutor {
private final Lock lock;
public SynchronizedExecutor() {
this.lock = new ReentrantLock();
}
SynchronizedExecutor(Lock lock) {
this.lock = lock;
}
public void execute(Runnable task) {
lock.lock();
try {
task.run();
} finally {
lock.unlock();
}
}
public R execute(Supplier task) {
lock.lock();
try {
return task.get();
} finally {
lock.unlock();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy