commit a8fc4660ac3b44cf00f4e58b46d97e0b5c6fcf42 Author: 2wenty1ne Date: Tue Dec 10 16:18:04 2024 +0100 Finished assigment diff --git a/Sum.java b/Sum.java new file mode 100644 index 0000000..fb89339 --- /dev/null +++ b/Sum.java @@ -0,0 +1,57 @@ + +/*- + +package main +import "fmt" +func tasker (o, r chan int, d chan bool) { + o <- 1; o <- 2 + fmt.Println (<-r); d <- true +} +func add (o, r chan int) { + r <- ((<-o) + (<-o)) +} +func main () { + operand, result := make(chan int), make(chan int) + done:= make(chan bool) + go tasker (operand, result, done) + go add (operand, result) + <-done +} + + */ + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +public class Sum { + public static void main(String... args) throws InterruptedException { + var operand = new LinkedBlockingQueue(); + var result = new LinkedBlockingQueue(); + var done = new LinkedBlockingQueue(); + + (new Thread(() -> tasker(operand, result, done))).start(); + (new Thread(() -> add(operand, result))).start(); + + done.take(); + } + + public static void tasker(BlockingQueue o, BlockingQueue r, BlockingQueue d) { + o.offer(3); + o.offer(2); + + try { + System.out.println(r.take()); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + d.offer(true); + } + + public static void add(BlockingQueue o, BlockingQueue r) { + try { + r.offer(Integer.valueOf(o.take()) + Integer.valueOf(o.take())); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } +}