Work on #403, removed checkstyle violations

This commit is contained in:
Narendra Pathai 2016-07-22 16:53:01 +05:30
parent 2b945ca27f
commit eb560f5f54
3 changed files with 5 additions and 98 deletions

View File

@ -15,7 +15,7 @@ public class App {
* Program entry point
* @param args arguments
* @throws InterruptedException if main thread is interruped.
* @throws ExecutionException
* @throws ExecutionException if an execution error occurs.
*/
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService executor = Executors.newSingleThreadExecutor();

View File

@ -3,9 +3,6 @@ package com.iluwatar.promise;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
import java.util.function.Function;
@ -147,96 +144,4 @@ public class Promise<T> extends PromiseSupport<T> {
}
}
}
}
/**
* A really simplified implementation of future that allows completing it successfully with a value
* or exceptionally with an exception.
*/
class PromiseSupport<T> implements Future<T> {
static final int RUNNING = 1;
static final int FAILED = 2;
static final int COMPLETED = 3;
final Object lock;
volatile int state = RUNNING;
T value;
Exception exception;
PromiseSupport() {
this.lock = new Object();
}
void fulfill(T value) {
this.value = value;
this.state = COMPLETED;
synchronized (lock) {
lock.notifyAll();
}
}
void fulfillExceptionally(Exception exception) {
this.exception = exception;
this.state = FAILED;
synchronized (lock) {
lock.notifyAll();
}
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return false;
}
@Override
public boolean isCancelled() {
return false;
}
@Override
public boolean isDone() {
return state > RUNNING;
}
@Override
public T get() throws InterruptedException, ExecutionException {
if (state == COMPLETED) {
return value;
} else if (state == FAILED) {
throw new ExecutionException(exception);
} else {
synchronized (lock) {
lock.wait();
if (state == COMPLETED) {
return value;
} else {
throw new ExecutionException(exception);
}
}
}
}
@Override
public T get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
if (state == COMPLETED) {
return value;
} else if (state == FAILED) {
throw new ExecutionException(exception);
} else {
synchronized (lock) {
lock.wait(unit.toMillis(timeout));
if (state == COMPLETED) {
return value;
} else if (state == FAILED) {
throw new ExecutionException(exception);
} else {
throw new TimeoutException();
}
}
}
}
}

View File

@ -58,7 +58,8 @@ public class PromiseTest {
@Override
public Integer call() throws Exception {
throw new RuntimeException("Barf!");
}}, executor);
}
}, executor);
try {
promise.get();
@ -85,7 +86,8 @@ public class PromiseTest {
@Override
public Integer call() throws Exception {
throw new RuntimeException("Barf!");
}}, executor);
}
}, executor);
try {
promise.get(1000, TimeUnit.SECONDS);