diff --git a/materials/2023-2024/11b/2024-04-15-routers/routers/.idea/.gitignore b/materials/2023-2024/11b/2024-04-15-routers/routers/.idea/.gitignore new file mode 100644 index 00000000..eaf91e2a --- /dev/null +++ b/materials/2023-2024/11b/2024-04-15-routers/routers/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/materials/2023-2024/11b/2024-04-15-routers/routers/.idea/compiler.xml b/materials/2023-2024/11b/2024-04-15-routers/routers/.idea/compiler.xml new file mode 100644 index 00000000..0e5c5cad --- /dev/null +++ b/materials/2023-2024/11b/2024-04-15-routers/routers/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-04-15-routers/routers/.idea/encodings.xml b/materials/2023-2024/11b/2024-04-15-routers/routers/.idea/encodings.xml new file mode 100644 index 00000000..aa00ffab --- /dev/null +++ b/materials/2023-2024/11b/2024-04-15-routers/routers/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-04-15-routers/routers/.idea/jarRepositories.xml b/materials/2023-2024/11b/2024-04-15-routers/routers/.idea/jarRepositories.xml new file mode 100644 index 00000000..a468a999 --- /dev/null +++ b/materials/2023-2024/11b/2024-04-15-routers/routers/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-04-15-routers/routers/.idea/misc.xml b/materials/2023-2024/11b/2024-04-15-routers/routers/.idea/misc.xml new file mode 100644 index 00000000..bf4a108d --- /dev/null +++ b/materials/2023-2024/11b/2024-04-15-routers/routers/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-04-15-routers/routers/.idea/vcs.xml b/materials/2023-2024/11b/2024-04-15-routers/routers/.idea/vcs.xml new file mode 100644 index 00000000..a5ef44da --- /dev/null +++ b/materials/2023-2024/11b/2024-04-15-routers/routers/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-04-15-routers/routers/pom.xml b/materials/2023-2024/11b/2024-04-15-routers/routers/pom.xml new file mode 100644 index 00000000..4e86de21 --- /dev/null +++ b/materials/2023-2024/11b/2024-04-15-routers/routers/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + org.example + routers + 1.0-SNAPSHOT + + + 19 + 19 + UTF-8 + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-04-15-routers/routers/src/main/java/Main.java b/materials/2023-2024/11b/2024-04-15-routers/routers/src/main/java/Main.java new file mode 100644 index 00000000..143c4b04 --- /dev/null +++ b/materials/2023-2024/11b/2024-04-15-routers/routers/src/main/java/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + + } +} diff --git a/materials/2023-2024/11b/2024-04-15-routers/routers/src/main/java/Router.java b/materials/2023-2024/11b/2024-04-15-routers/routers/src/main/java/Router.java new file mode 100644 index 00000000..b0274fc4 --- /dev/null +++ b/materials/2023-2024/11b/2024-04-15-routers/routers/src/main/java/Router.java @@ -0,0 +1,98 @@ +import java.util.*; + +public class Router implements Runnable { + private List other = new LinkedList<>(); + private List hosts = new LinkedList<>(); + + private Queue packets = new LinkedList<>(); + private Queue waitingPackets = new LinkedList<>(); + + private Map arpTable = new TreeMap<>(); + +// synchronized public List getOther() { +// return other; +// } + + @Override + public void run() { + try { + while(true) { + while(!packets.isEmpty()) { + Object packet = packets.remove(); + processPacket(packet); + Thread.sleep(500); + } + + synchronized (this) { + wait(); + } + } + } catch (InterruptedException e) { + System.out.println("Router shutting down"); + } + } + + private void processPacket(Object packet) { + String packetType = "data"; + + switch (packetType) { + case "data": + processDataPacket(packet); + break; + case "query": + processQueryPacket(packet); + break; + } + } + + private void processQueryPacket(Object packet) { + String destination = "H3"; + String srcRouter = "R1"; + + Object host = findHost(destination); + if(host != null) { + Router r1; // = ??? + r1.receivePacket("type=found, destination=H3"); + return; + } + } + + private Object findHost(String destination) { + for(Object host : hosts) { + String hostName = "H3"; + if(hostName.equals(destination)) { + return host; + } + } + + return null; + } + + private void processDataPacket(Object packet) { + String destination = "H3"; + + for(Object host : hosts) { + String hostName = "H3"; + if(hostName.equals(destination)) { + System.out.println("host.receivePacket(packet);"); + return; + } + } + + if(arpTable.containsKey(destination)) { + Integer neighborIndex = arpTable.get(destination); + other.get(neighborIndex).receivePacket(packet); + return; + } + + for(Router neighbor : other) { + neighbor.receivePacket("type: query, destination: H3"); + } + + waitingPackets.add(packet); + } + + private void receivePacket(Object packet) { + + } +} diff --git a/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/.idea/.gitignore b/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/.idea/.gitignore new file mode 100644 index 00000000..eaf91e2a --- /dev/null +++ b/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/.idea/compiler.xml b/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/.idea/compiler.xml new file mode 100644 index 00000000..fee1d363 --- /dev/null +++ b/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/.idea/encodings.xml b/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/.idea/encodings.xml new file mode 100644 index 00000000..aa00ffab --- /dev/null +++ b/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/.idea/jarRepositories.xml b/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/.idea/jarRepositories.xml new file mode 100644 index 00000000..a468a999 --- /dev/null +++ b/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/.idea/misc.xml b/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/.idea/misc.xml new file mode 100644 index 00000000..bf4a108d --- /dev/null +++ b/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/.idea/vcs.xml b/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/.idea/vcs.xml new file mode 100644 index 00000000..a5ef44da --- /dev/null +++ b/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/pom.xml b/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/pom.xml new file mode 100644 index 00000000..a81e3815 --- /dev/null +++ b/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + org.example + executor-service-2 + 1.0-SNAPSHOT + + + 19 + 19 + UTF-8 + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/src/main/java/Main.java b/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/src/main/java/Main.java new file mode 100644 index 00000000..f4e30ae4 --- /dev/null +++ b/materials/2023-2024/11b/2024-04-29-executor-service-2/executor-service-2/src/main/java/Main.java @@ -0,0 +1,60 @@ +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.*; + +public class Main { + public static class MyTask implements Callable { + private int task; + + public MyTask(int task) { + this.task = task; + } + +// @Override +// public void run() { +// try { +// Thread.sleep(5000); +// } catch (InterruptedException e) { +// throw new RuntimeException(e); +// } +// } + + @Override + public Integer call() throws Exception { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + return task; + } + } + + public static void main(String[] args) throws ExecutionException, InterruptedException { +// ExecutorService ex = Executors.newFixedThreadPool(2); +// ExecutorService ex = Executors.newCachedThreadPool(); + ExecutorService ex = Executors.newWorkStealingPool() + + List> results = new LinkedList<>(); + for(int i = 0; i < 10; i++) { +// ex.execute(new MyTask(i)); +// Future res = ex.submit(new MyTask(i)); +// Integer finalRes = res.get(); +// System.out.println(finalRes); + results.add(ex.submit(new MyTask(i))); + } + + results.stream().map(future -> { + try { + return future.get(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } catch (ExecutionException e) { + throw new RuntimeException(e); + } + }).forEach(System.out::println); + + ex.shutdown(); + } +} diff --git a/materials/2023-2024/11b/2024-05-28-exercise/exercise/.idea/.gitignore b/materials/2023-2024/11b/2024-05-28-exercise/exercise/.idea/.gitignore new file mode 100644 index 00000000..eaf91e2a --- /dev/null +++ b/materials/2023-2024/11b/2024-05-28-exercise/exercise/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/materials/2023-2024/11b/2024-05-28-exercise/exercise/.idea/compiler.xml b/materials/2023-2024/11b/2024-05-28-exercise/exercise/.idea/compiler.xml new file mode 100644 index 00000000..86b8cd19 --- /dev/null +++ b/materials/2023-2024/11b/2024-05-28-exercise/exercise/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-05-28-exercise/exercise/.idea/encodings.xml b/materials/2023-2024/11b/2024-05-28-exercise/exercise/.idea/encodings.xml new file mode 100644 index 00000000..aa00ffab --- /dev/null +++ b/materials/2023-2024/11b/2024-05-28-exercise/exercise/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-05-28-exercise/exercise/.idea/jarRepositories.xml b/materials/2023-2024/11b/2024-05-28-exercise/exercise/.idea/jarRepositories.xml new file mode 100644 index 00000000..a468a999 --- /dev/null +++ b/materials/2023-2024/11b/2024-05-28-exercise/exercise/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-05-28-exercise/exercise/.idea/misc.xml b/materials/2023-2024/11b/2024-05-28-exercise/exercise/.idea/misc.xml new file mode 100644 index 00000000..bf4a108d --- /dev/null +++ b/materials/2023-2024/11b/2024-05-28-exercise/exercise/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-05-28-exercise/exercise/.idea/vcs.xml b/materials/2023-2024/11b/2024-05-28-exercise/exercise/.idea/vcs.xml new file mode 100644 index 00000000..a5ef44da --- /dev/null +++ b/materials/2023-2024/11b/2024-05-28-exercise/exercise/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-05-28-exercise/exercise/pom.xml b/materials/2023-2024/11b/2024-05-28-exercise/exercise/pom.xml new file mode 100644 index 00000000..671c7f53 --- /dev/null +++ b/materials/2023-2024/11b/2024-05-28-exercise/exercise/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + org.example + exercise + 1.0-SNAPSHOT + + + 19 + 19 + UTF-8 + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-05-28-exercise/exercise/src/main/java/Main.java b/materials/2023-2024/11b/2024-05-28-exercise/exercise/src/main/java/Main.java new file mode 100644 index 00000000..5fa3efcb --- /dev/null +++ b/materials/2023-2024/11b/2024-05-28-exercise/exercise/src/main/java/Main.java @@ -0,0 +1,85 @@ +import java.util.LinkedList; +import java.util.List; + +public class Main { +// public: +// const int operator+(const Main& other) const +// { +// const int val = 5; +// const int val2; +// val2 = 5; +// +// int val3 = 5; +// const int val4 = val3; +// val3 = val4; +// val4 = 10; +// +// (const int*) const val5; +// } +// } + + public int val1 = 5; + public int val4; + public final int val2 = 6; + public final int val3; + public final List val5 = new LinkedList<>(); + + public Main(int arg3) { + val3 = arg3; + //val3 = arg3; + //val2 = 5; + } + + public final List getVal5() { + return val5; + } + + public void baz() { + List val6 = getVal5(); + val6 = new LinkedList<>(); +// getVal5() = new LinkedList<>(); + } + + public void bad(final Main arg) { + arg = new Main(5); + arg.val1 = 10; + + final List val7 = new LinkedList<>(); + val7.add(arg.val1); + val7 = val5; + } + + public void foo() { + val1 = 55; + val4 = 77; + + val5.add(5); + val5.remove(5); + val5.clear(); + val5 = new LinkedList<>(); + + val2 = 66; + val3 = 88; + } + + public final void bar() { + val1 = 55; + val4 = 77; + val2 = 66; + val3 = 88; + } +} + +public class Main2 extends Main { + @Override + public void bar() { + } + + +} + + + + + + diff --git a/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/.gitignore b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/.gitignore new file mode 100644 index 00000000..eaf91e2a --- /dev/null +++ b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/compiler.xml b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/compiler.xml new file mode 100644 index 00000000..d0c2ab09 --- /dev/null +++ b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/encodings.xml b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/encodings.xml new file mode 100644 index 00000000..aa00ffab --- /dev/null +++ b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/jarRepositories.xml b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/jarRepositories.xml new file mode 100644 index 00000000..a468a999 --- /dev/null +++ b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/misc.xml b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/misc.xml new file mode 100644 index 00000000..bf4a108d --- /dev/null +++ b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/uiDesigner.xml b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/uiDesigner.xml new file mode 100644 index 00000000..6d50cd4d --- /dev/null +++ b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/vcs.xml b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/vcs.xml new file mode 100644 index 00000000..a5ef44da --- /dev/null +++ b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/pom.xml b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/pom.xml new file mode 100644 index 00000000..2ba8bdda --- /dev/null +++ b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + + org.example + homework-2 + 1.0-SNAPSHOT + + + 19 + 19 + UTF-8 + + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/src/main/java/Foo.java b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/src/main/java/Foo.java new file mode 100644 index 00000000..18301caa --- /dev/null +++ b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/src/main/java/Foo.java @@ -0,0 +1,30 @@ +public class Foo implements Runnable { + public int var = 5; + + public Foo() {} + + public Foo(int var) { + this.var = var; + } + + public static Foo factoryMethod1() { + return new Foo(); + } + + public static Foo factoryMethod2() { + return new Foo(15); + } + + public void bar(int arg) throws Exception { + if(arg > 5) throw new Exception("My exception"); + } + + @Override + public void run() { + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } +} diff --git a/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/src/main/java/Main.java b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/src/main/java/Main.java new file mode 100644 index 00000000..a27d6d04 --- /dev/null +++ b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/src/main/java/Main.java @@ -0,0 +1,13 @@ +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +public class Main { + ExecutorService ex = Executors.newFixedThreadPool(5); + public void main() { +// Foo f1 = Foo.factoryMethod1(); +// Foo f2 = Foo.factoryMethod2(); + ex.execute(() -> {}); + ex.execute(() -> {}); + ex.execute(() -> {}); + } +} diff --git a/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/src/test/java/FooTest.java b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/src/test/java/FooTest.java new file mode 100644 index 00000000..03971ac6 --- /dev/null +++ b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/src/test/java/FooTest.java @@ -0,0 +1,48 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class FooTest { + + Foo foo; + + @BeforeEach + void setUp() { + foo = new Foo(); + } + + @Test + void barThrowsAnError() { + assertThrows(Exception.class, () -> foo.bar(6)); + assertThrowsExactly(Exception.class, () -> foo.bar(6)); + assertDoesNotThrow(() -> foo.bar(4)); + +// Exception res = null; +// +// try { +// foo.bar(6); +// } catch (Exception err) { +// res = err; +// } +// +// assertNotEquals(null, res); +// assertInstanceOf(Exception.class, res); +// assertEquals("My exception", res.getMessage()); + } + + @Test + void run() { + mock(Thread); + when(Thread.sleep).then(() -> { + // do nothing + }); + assertDoesNotThrow(() -> foo.run()); + assertCalledWith(Thread.sleep, 10000); + + when(Thread.sleep).then(() -> { + throw new InterruptedException(); + }); + assertThrows(RuntimeException.class, () -> foo.run()); + } +} \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/src/test/java/MainTest.java b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/src/test/java/MainTest.java new file mode 100644 index 00000000..7ef9173c --- /dev/null +++ b/materials/2023-2024/11b/2024-05-31-homework-2/homework-2/src/test/java/MainTest.java @@ -0,0 +1,20 @@ +import org.junit.jupiter.api.Test; + +import java.util.concurrent.ExecutorService; + +import static org.junit.jupiter.api.Assertions.*; + +class MainTest { + + @mock + ExecutorService ex; + + @Test + void main() { + Main m = new Main(); + m.ex = ex; + + m.main(); + assertEquals(3, ex.submit.calls); + } +} \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/.idea/.gitignore b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/.idea/.gitignore new file mode 100644 index 00000000..eaf91e2a --- /dev/null +++ b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/.idea/compiler.xml b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/.idea/compiler.xml new file mode 100644 index 00000000..d4cb2848 --- /dev/null +++ b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/.idea/encodings.xml b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/.idea/encodings.xml new file mode 100644 index 00000000..aa00ffab --- /dev/null +++ b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/.idea/jarRepositories.xml b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/.idea/jarRepositories.xml new file mode 100644 index 00000000..a468a999 --- /dev/null +++ b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/.idea/misc.xml b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/.idea/misc.xml new file mode 100644 index 00000000..bf4a108d --- /dev/null +++ b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/.idea/vcs.xml b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/.idea/vcs.xml new file mode 100644 index 00000000..a5ef44da --- /dev/null +++ b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/pom.xml b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/pom.xml new file mode 100644 index 00000000..a96a29a2 --- /dev/null +++ b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + org.example + homework-5 + 1.0-SNAPSHOT + + + 19 + 19 + UTF-8 + + + \ No newline at end of file diff --git a/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/src/main/java/Cook.java b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/src/main/java/Cook.java new file mode 100644 index 00000000..95ea506e --- /dev/null +++ b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/src/main/java/Cook.java @@ -0,0 +1,51 @@ +import java.util.Queue; + +public class Cook implements Runnable { + private String name; + private Order order = null; + private Restaurant restaurant; + + public Cook(String name, Restaurant restaurant) { + this.name = name; + this.restaurant = restaurant; + } + + public String getName() { + return name; + } + + @Override + public void run() { + while(true) { + if(Thread.interrupted()) break; + + try { + waitAndTakeOrder(); + cookOrder(); + } catch (InterruptedException e) { + break; + } + + finalizeOrder(); + } + } + + private void finalizeOrder() { + restaurant.finalizeOrder(order); + order = null; + } + + private void cookOrder() throws InterruptedException { + Thread.sleep(3000); + } + + private void waitAndTakeOrder() throws InterruptedException { + Queue orders = restaurant.getOrders(); + synchronized (orders) { + if(orders.isEmpty()) { + orders.wait(); + } + order = orders.remove(); + } + } +} diff --git a/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/src/main/java/Main.java b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/src/main/java/Main.java new file mode 100644 index 00000000..7ab812f3 --- /dev/null +++ b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/src/main/java/Main.java @@ -0,0 +1,2 @@ +public class Main { +} diff --git a/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/src/main/java/Order.java b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/src/main/java/Order.java new file mode 100644 index 00000000..0c55c35e --- /dev/null +++ b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/src/main/java/Order.java @@ -0,0 +1,11 @@ +public class Order { + private int id; + + public Order(int id) { + this.id = id; + } + + public int getId() { + return id; + } +} diff --git a/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/src/main/java/Restaurant.java b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/src/main/java/Restaurant.java new file mode 100644 index 00000000..f1ef6b09 --- /dev/null +++ b/materials/2023-2024/11b/2024-06-03-homework-5/homework-5/src/main/java/Restaurant.java @@ -0,0 +1,41 @@ +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class Restaurant { + private final Queue orders = new LinkedList<>(); + private List allOrders = new LinkedList<>(); + + public void addOrder(Order order) { + synchronized (orders) { + orders.add(order); + allOrders.add(order); + + orders.notify(); + } + } + + public void finalizeOrder(Order order) throws Exception { + synchronized (orders) { + if(allOrders + .stream() + .noneMatch((o) -> o.getId() == order.getId()) +// .anyMatch((o) -> o.getId() == order.getId()) + ) { + throw new Exception("Invalid order"); + } + + System.out.println("Order " + order.getId() + " ready"); + allOrders = allOrders + .stream() + .filter((o) -> o.getId() != order.getId()) + .toList(); + +// allOrders.remove(allOrders.indexOf(order)); + } + } + + public Queue getOrders() { + return orders; + } +}