|
| 1 | +package com.iluwatar.async.method.invocation; |
| 2 | + |
| 3 | +import java.util.Optional; |
| 4 | +import java.util.concurrent.Callable; |
| 5 | +import java.util.concurrent.ExecutionException; |
| 6 | +import java.util.concurrent.atomic.AtomicInteger; |
| 7 | + |
| 8 | +public class ThreadAsyncExecutor implements AsyncExecutor { |
| 9 | + |
| 10 | + private final AtomicInteger idx = new AtomicInteger(0); |
| 11 | + |
| 12 | + @Override |
| 13 | + public <T> AsyncResult<T> startProcess(Callable<T> task) { |
| 14 | + return startProcess(task, null); |
| 15 | + } |
| 16 | + |
| 17 | + @Override |
| 18 | + public <T> AsyncResult<T> startProcess(Callable<T> task, AsyncCallback<T> callback) { |
| 19 | + CompletableResult<T> result = new CompletableResult<>(callback); |
| 20 | + new Thread(() -> { |
| 21 | + try { |
| 22 | + result.setValue(task.call()); |
| 23 | + } catch (Exception ex) { |
| 24 | + result.setException(ex); |
| 25 | + } |
| 26 | + }, "executor-" + idx.incrementAndGet()).start(); |
| 27 | + return result; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public <T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException, InterruptedException { |
| 32 | + if (asyncResult.isCompleted()) { |
| 33 | + return asyncResult.getValue(); |
| 34 | + } else { |
| 35 | + asyncResult.await(); |
| 36 | + return asyncResult.getValue(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + private static class CompletableResult<T> implements AsyncResult<T> { |
| 41 | + |
| 42 | + static final int RUNNING = 1; |
| 43 | + static final int FAILED = 2; |
| 44 | + static final int COMPLETED = 3; |
| 45 | + |
| 46 | + final Object lock; |
| 47 | + final Optional<AsyncCallback<T>> callback; |
| 48 | + |
| 49 | + volatile int state = RUNNING; |
| 50 | + T value; |
| 51 | + Exception exception; |
| 52 | + |
| 53 | + CompletableResult(AsyncCallback<T> callback) { |
| 54 | + this.lock = new Object(); |
| 55 | + this.callback = Optional.ofNullable(callback); |
| 56 | + } |
| 57 | + |
| 58 | + void setValue(T value) { |
| 59 | + this.value = value; |
| 60 | + this.state = COMPLETED; |
| 61 | + this.callback.ifPresent(ac -> ac.onComplete(value, Optional.<Exception>empty())); |
| 62 | + synchronized (lock) { |
| 63 | + lock.notifyAll(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + void setException(Exception exception) { |
| 68 | + this.exception = exception; |
| 69 | + this.state = FAILED; |
| 70 | + this.callback.ifPresent(ac -> ac.onComplete(null, Optional.of(exception))); |
| 71 | + synchronized (lock) { |
| 72 | + lock.notifyAll(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean isCompleted() { |
| 78 | + return (state > RUNNING); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public T getValue() throws ExecutionException { |
| 83 | + if (state == COMPLETED) { |
| 84 | + return value; |
| 85 | + } else if (state == FAILED) { |
| 86 | + throw new ExecutionException(exception); |
| 87 | + } else { |
| 88 | + throw new IllegalStateException("Execution not completed yet"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void await() throws InterruptedException { |
| 94 | + synchronized (lock) { |
| 95 | + if (!isCompleted()) { |
| 96 | + lock.wait(); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments