main
commit
6f258990d4
|
@ -0,0 +1,4 @@
|
|||
@FunctionalInterface
|
||||
public interface Expression<T> {
|
||||
public T eval();
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
public class Main {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
RunnableWithResult<Integer> run1 = new RunnableWithResult(() -> 1+2);
|
||||
Thread expr1 = new Thread(run1);
|
||||
|
||||
RunnableWithResult<Integer> run2 = new RunnableWithResult(() -> 3+4);
|
||||
Thread expr2 = new Thread(run2);
|
||||
|
||||
expr1.start();
|
||||
expr2.start();
|
||||
|
||||
expr1.join();
|
||||
expr2.join();
|
||||
|
||||
System.out.printf("Res: %s \n", run1.get().toString());
|
||||
System.out.printf("Res: %s \n", run2.get().toString());
|
||||
|
||||
RunnableWithResult run3;
|
||||
Thread expr3;
|
||||
|
||||
if (run1.isAvailable() && run2.isAvailable()) {
|
||||
run3 = new RunnableWithResult(() -> run1.get().intValue() + run2.get().intValue());
|
||||
expr3 = new Thread(run3);
|
||||
expr3.start();
|
||||
expr3.join();
|
||||
System.out.printf("Res: %s \n", run3.get().toString());
|
||||
}
|
||||
|
||||
|
||||
//RunnableWithResult<Integer> expr1 = new RunnableWithResult(() -> 1+2);
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
public class RunnableWithResult<T> implements Runnable {
|
||||
private final Expression<T> expr;
|
||||
private T result;
|
||||
|
||||
public RunnableWithResult(Expression<T> expr) {
|
||||
this.expr = expr;
|
||||
this.result = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
this.result = this.expr.eval();
|
||||
}
|
||||
|
||||
public synchronized Boolean isAvailable() {
|
||||
return result != null;
|
||||
}
|
||||
|
||||
public synchronized T get() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public Expression<T> expr() {
|
||||
return expr;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue