commit 6f258990d412cf69d93112ef3ec7d3a0ca29d5da Author: 2wenty1ne Date: Tue Oct 29 14:44:01 2024 +0100 c diff --git a/src/src/Expression.java b/src/src/Expression.java new file mode 100644 index 0000000..d8f99df --- /dev/null +++ b/src/src/Expression.java @@ -0,0 +1,4 @@ +@FunctionalInterface +public interface Expression { + public T eval(); +} \ No newline at end of file diff --git a/src/src/Main.java b/src/src/Main.java new file mode 100644 index 0000000..b08f702 --- /dev/null +++ b/src/src/Main.java @@ -0,0 +1,33 @@ +public class Main { + public static void main(String[] args) throws InterruptedException { + RunnableWithResult run1 = new RunnableWithResult(() -> 1+2); + Thread expr1 = new Thread(run1); + + RunnableWithResult 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 expr1 = new RunnableWithResult(() -> 1+2); + + } +} \ No newline at end of file diff --git a/src/src/RunnableWithResult.java b/src/src/RunnableWithResult.java new file mode 100644 index 0000000..2ca4ef0 --- /dev/null +++ b/src/src/RunnableWithResult.java @@ -0,0 +1,26 @@ +public class RunnableWithResult implements Runnable { + private final Expression expr; + private T result; + + public RunnableWithResult(Expression 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 expr() { + return expr; + } +} \ No newline at end of file