pro.jk.ejoker.common.system.wrapper.FutureTaskWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ejoker-common Show documentation
Show all versions of ejoker-common Show documentation
EJoker is a CQRS + EventSourcing framwork
package pro.jk.ejoker.common.system.wrapper;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.concurrent.locks.Lock;
/**
* 重寫兩個get方法讓他們能支持thread和quasar fiber的同步
*
* @author JiefzzLon
*
* @param
*/
public class FutureTaskWrapper extends java.util.concurrent.FutureTask {
private Object awaitHandle = CountDownLatchWrapper.newCountDownLatch();
public FutureTaskWrapper(Callable callable) {
super(callable);
}
public FutureTaskWrapper(Runnable runnable, V result) {
super(runnable, result);
}
public void release() {
CountDownLatchWrapper.countDown(awaitHandle);
}
@Override
public V get() throws InterruptedException, ExecutionException {
CountDownLatchWrapper.await(awaitHandle);
return super.get();
}
@Override
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
if (!CountDownLatchWrapper.await(awaitHandle, timeout, unit)) {
if(!this.isDone())
throw new TimeoutException();
}
return super.get();
}
}