From fbe135a09bc48a8004cd3998c8bda3cd4fed4bff Mon Sep 17 00:00:00 2001 From: tkuzub Date: Sun, 3 Oct 2021 12:56:37 +0300 Subject: [PATCH 01/12] GP-106 Add README.md --- 7-0-java-concurrency/7-0-0-hello-threads/README.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 7-0-java-concurrency/7-0-0-hello-threads/README.md diff --git a/7-0-java-concurrency/7-0-0-hello-threads/README.md b/7-0-java-concurrency/7-0-0-hello-threads/README.md new file mode 100644 index 000000000..c698aaa65 --- /dev/null +++ b/7-0-java-concurrency/7-0-0-hello-threads/README.md @@ -0,0 +1,7 @@ +# Java concurrency +Start learning concurrency in Java by writing simple functions using Threads πŸ’ͺ + +### Objectives +TODO +#### πŸ†• First time here? – [See Introduction](https://github.com/bobocode-projects/java-fundamentals-course/tree/main/0-0-intro#introduction) +#### ➑️ Have any feedback? – [Please fill the form ](https://forms.gle/TPSCpZAMZvNXYCoA6) \ No newline at end of file From 2a18af966ba0eb18b2433c65e5809f4315122ddf Mon Sep 17 00:00:00 2001 From: tkuzub Date: Sun, 3 Oct 2021 13:00:04 +0300 Subject: [PATCH 02/12] GP-106 Add pom and module concurrency main pom --- .../7-0-0-hello-threads/pom.xml | 19 +++++++++++++++++++ 7-0-java-concurrency/pom.xml | 17 +++++++++++++++++ pom.xml | 2 ++ 3 files changed, 38 insertions(+) create mode 100644 7-0-java-concurrency/7-0-0-hello-threads/pom.xml create mode 100644 7-0-java-concurrency/pom.xml diff --git a/7-0-java-concurrency/7-0-0-hello-threads/pom.xml b/7-0-java-concurrency/7-0-0-hello-threads/pom.xml new file mode 100644 index 000000000..8c17e6eda --- /dev/null +++ b/7-0-java-concurrency/7-0-0-hello-threads/pom.xml @@ -0,0 +1,19 @@ + + + + java-fundamentals-course + com.bobocode + 1.0-SNAPSHOT + ../../pom.xml + + 4.0.0 + + 7-0-0-hello-theads + + + 11 + 11 + + \ No newline at end of file diff --git a/7-0-java-concurrency/pom.xml b/7-0-java-concurrency/pom.xml new file mode 100644 index 000000000..b4437da18 --- /dev/null +++ b/7-0-java-concurrency/pom.xml @@ -0,0 +1,17 @@ + + + + java-fundamentals-course + com.bobocode + 1.0-SNAPSHOT + + 4.0.0 + + 7-0-java-concurrency + + 11 + 11 + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 824dae046..031798b9d 100644 --- a/pom.xml +++ b/pom.xml @@ -25,6 +25,8 @@ 6-0-test-driven-development java-fundamentals-util lesson-demo + 7-0-java-concurrency + 7-0-java-concurrency/7-0-0-hello-threads From ff7238b813b9a2048e69c5c1444837de0f272e95 Mon Sep 17 00:00:00 2001 From: tkuzub Date: Sun, 3 Oct 2021 13:02:34 +0300 Subject: [PATCH 03/12] GP-106 Add new task for 7-0-0-hello-threads --- .../src/main/java/HelloThreads.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java diff --git a/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java b/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java new file mode 100644 index 000000000..c627057a1 --- /dev/null +++ b/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java @@ -0,0 +1,45 @@ +public class HelloThreads { + /** + * Receives a {@link Runnable} parameter, and returns a {@link Thread}. + * @param runnable the code you want to tun in new thread + * @return a new thread + */ + public static Thread runningThread(Runnable runnable) { + throw new RuntimeException(); + } + + /** + * Receive a {@link Thread} parameter and start it + * @param thread the code you want to start thread + */ + public static void runningThreadViaStart(Thread thread) { + thread.start(); + } + + /** + * Receives a {@link Thread} parameter, and return a {@link String} + * @param thread the code you want start thread and return tread name + * @return the name thread + */ + public static String runningThreadGetNameThread(Thread thread) { + throw new RuntimeException(); + } + + /** + * Receives a {@link Thread} parameter, and return a {@link Thread.State} + * @param thread the code you want start thread and return state + * @return the thread state + */ + public static Thread.State runningThreadGetStateThread(Thread thread) { + throw new RuntimeException(); + } + + /** + * Receives a {@link Runnable} parameter, create new {@link Thread}, start and return this {@link Thread} + * @param runnable the code you want to tun in new thread and start + * @return this thread + */ + public static Thread getSomeLogicRunningTreadAndReturnThisThread(Runnable runnable) { + throw new RuntimeException(); + } +} From 9f5450efe85c7123f59d8bc47b922f0250c05b88 Mon Sep 17 00:00:00 2001 From: tkuzub Date: Sun, 3 Oct 2021 13:03:10 +0300 Subject: [PATCH 04/12] GP-106 add test for 7-0-0-hello-threads --- .../src/test/java/HelloThreadsTest.java | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java diff --git a/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java b/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java new file mode 100644 index 000000000..5425684ae --- /dev/null +++ b/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java @@ -0,0 +1,78 @@ +import org.junit.jupiter.api.*; + +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; + +import static org.junit.jupiter.api.Assertions.*; + +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class HelloThreadsTest { + + private Queue concurrentLinkedQueue; + + @BeforeEach + void prepare() { + concurrentLinkedQueue = new ConcurrentLinkedQueue(); + } + + @Test + @Order(1) + void runningThread() throws InterruptedException { + Thread runningThread = HelloThreads.runningThread(() -> concurrentLinkedQueue.add(5)); + assertEquals(0, concurrentLinkedQueue.size()); + + runningThread.start(); + runningThread.join(); + + checkContentQueue(); + } + + @Test + @Order(2) + void runningThreadViaStart() throws InterruptedException { + var thread = new Thread(() -> concurrentLinkedQueue.add(5)); + HelloThreads.runningThreadViaStart(thread); + thread.join(); + + checkContentQueue(); + } + + @Test + @Order(3) + void runningThreadGetNameThread() throws InterruptedException { + var thread = new Thread(() -> concurrentLinkedQueue.add(5), "name"); + var threadName = HelloThreads.runningThreadGetNameThread(thread); + thread.join(); + assertEquals("name", threadName); + + checkContentQueue(); + } + + @Test + @Order(3) + void runningThreadGetStateThread() throws InterruptedException { + var thread = new Thread(() -> concurrentLinkedQueue.add(5)); + var state = HelloThreads.runningThreadGetStateThread(thread); + thread.join(); + assertSame(state, Thread.State.RUNNABLE); + + checkContentQueue(); + } + + @Test + @Order(4) + void getSomeLogicRunningTreadAndReturnThisThread() throws InterruptedException { + var thread = HelloThreads.getSomeLogicRunningTreadAndReturnThisThread(new Thread(() -> { + assertEquals(0, concurrentLinkedQueue.size()); + concurrentLinkedQueue.add(5); + })); + thread.join(); + + checkContentQueue(); + } + + private void checkContentQueue() { + assertEquals(1, concurrentLinkedQueue.size()); + assertEquals(5, concurrentLinkedQueue.element().intValue()); + } +} \ No newline at end of file From 234e63a36cbdd383cef1fbf85befbb71eed98ab8 Mon Sep 17 00:00:00 2001 From: tkuzub Date: Sun, 3 Oct 2021 13:09:12 +0300 Subject: [PATCH 05/12] GP-106 add solution for 7-0-0-hello-thread --- .../src/main/java/HelloThreads.java | 54 ++++++++++++++++--- .../src/test/java/HelloThreadsTest.java | 54 +++++++++++++++---- 2 files changed, 93 insertions(+), 15 deletions(-) diff --git a/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java b/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java index c627057a1..a22648f55 100644 --- a/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java +++ b/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java @@ -1,11 +1,13 @@ +import lombok.SneakyThrows; + public class HelloThreads { /** * Receives a {@link Runnable} parameter, and returns a {@link Thread}. - * @param runnable the code you want to tun in new thread + * @param runnable the code you want to run in new thread * @return a new thread */ public static Thread runningThread(Runnable runnable) { - throw new RuntimeException(); + return new Thread(runnable); } /** @@ -22,7 +24,8 @@ public static void runningThreadViaStart(Thread thread) { * @return the name thread */ public static String runningThreadGetNameThread(Thread thread) { - throw new RuntimeException(); + thread.start(); + return thread.getName(); } /** @@ -31,15 +34,54 @@ public static String runningThreadGetNameThread(Thread thread) { * @return the thread state */ public static Thread.State runningThreadGetStateThread(Thread thread) { - throw new RuntimeException(); + thread.start(); + return thread.getState(); } /** * Receives a {@link Runnable} parameter, create new {@link Thread}, start and return this {@link Thread} - * @param runnable the code you want to tun in new thread and start + * @param runnable the code you want to run in new thread and start * @return this thread */ public static Thread getSomeLogicRunningTreadAndReturnThisThread(Runnable runnable) { - throw new RuntimeException(); + var thread = new Thread(runnable); + thread.start(); + return thread; + } + + /** + * Receives a {@link Runnable} parameter, create new {@link Thread}, start and wait when it has completed + * @param runnable - the code you want to run in new thread and start + */ + @SneakyThrows + public static void runningThreadAndWhenJoinCompleted(Runnable runnable) { + var thread = new Thread(runnable); + thread.start(); + thread.join(); + } + + // accept a list of tasks (Runnable) and return a list of threads that are in progress + @SneakyThrows + public static void runningMultipleThreadsWithOneTask(Runnable runnable) { + var thread1 = new Thread(runnable); + var thread2 = new Thread(runnable); + var thread3 = new Thread(runnable); + + thread1.start(); + thread2.start(); + thread3.start(); + + thread1.join(); + thread2.join(); + thread3.join(); + } + + // accept a thread and make it sleep + @SneakyThrows + public static void runningThreadAndMakeItSleep(Thread thread) { + +// thread.start(); +// Thread.sleep(10000); +// thread.join(); } } diff --git a/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java b/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java index 5425684ae..03e496ac0 100644 --- a/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java +++ b/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java @@ -1,7 +1,10 @@ +import lombok.SneakyThrows; import org.junit.jupiter.api.*; +import java.util.List; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ThreadLocalRandom; import static org.junit.jupiter.api.Assertions.*; @@ -14,10 +17,10 @@ class HelloThreadsTest { void prepare() { concurrentLinkedQueue = new ConcurrentLinkedQueue(); } - + @SneakyThrows @Test @Order(1) - void runningThread() throws InterruptedException { + void runningThread() { Thread runningThread = HelloThreads.runningThread(() -> concurrentLinkedQueue.add(5)); assertEquals(0, concurrentLinkedQueue.size()); @@ -26,20 +29,20 @@ void runningThread() throws InterruptedException { checkContentQueue(); } - + @SneakyThrows @Test @Order(2) - void runningThreadViaStart() throws InterruptedException { + void runningThreadViaStart() { var thread = new Thread(() -> concurrentLinkedQueue.add(5)); HelloThreads.runningThreadViaStart(thread); thread.join(); checkContentQueue(); } - + @SneakyThrows @Test @Order(3) - void runningThreadGetNameThread() throws InterruptedException { + void runningThreadGetNameThread() { var thread = new Thread(() -> concurrentLinkedQueue.add(5), "name"); var threadName = HelloThreads.runningThreadGetNameThread(thread); thread.join(); @@ -47,10 +50,10 @@ void runningThreadGetNameThread() throws InterruptedException { checkContentQueue(); } - + @SneakyThrows @Test @Order(3) - void runningThreadGetStateThread() throws InterruptedException { + void runningThreadGetStateThread() { var thread = new Thread(() -> concurrentLinkedQueue.add(5)); var state = HelloThreads.runningThreadGetStateThread(thread); thread.join(); @@ -59,9 +62,10 @@ void runningThreadGetStateThread() throws InterruptedException { checkContentQueue(); } + @SneakyThrows @Test @Order(4) - void getSomeLogicRunningTreadAndReturnThisThread() throws InterruptedException { + void getSomeLogicRunningTreadAndReturnThisThread() { var thread = HelloThreads.getSomeLogicRunningTreadAndReturnThisThread(new Thread(() -> { assertEquals(0, concurrentLinkedQueue.size()); concurrentLinkedQueue.add(5); @@ -71,6 +75,38 @@ void getSomeLogicRunningTreadAndReturnThisThread() throws InterruptedException { checkContentQueue(); } + @SneakyThrows + @Test + @Order(5) + void runningThreadAndWhenJoinCompleted() { + HelloThreads.runningThreadAndWhenJoinCompleted(new Thread(() -> { + assertEquals(0, concurrentLinkedQueue.size()); + concurrentLinkedQueue.add(5); + })); + + checkContentQueue(); + } + + @SneakyThrows + @Test + @Order(6) + void runningMultipleThreadsWithOneTask() { + HelloThreads.runningMultipleThreadsWithOneTask(new Thread(() -> + concurrentLinkedQueue.add(ThreadLocalRandom.current().nextInt(100)))); + + assertEquals(3, concurrentLinkedQueue.size()); + } + + @SneakyThrows + @Test + @Order(7) + void runningThreadAndMakeItSleep() { + var thread = new Thread(() -> concurrentLinkedQueue.add(5)); + HelloThreads.runningThreadAndMakeItSleep(thread); + + assertSame(thread.getState(), Thread.State.RUNNABLE); + } + private void checkContentQueue() { assertEquals(1, concurrentLinkedQueue.size()); assertEquals(5, concurrentLinkedQueue.element().intValue()); From 41c3d07f9dded957da00af47888503c850bf41c4 Mon Sep 17 00:00:00 2001 From: tkuzub Date: Thu, 7 Oct 2021 10:26:40 +0300 Subject: [PATCH 06/12] GP-106 add new task to 7-0-0-hello-threads --- .../src/main/java/HelloThreads.java | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java b/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java index a22648f55..70b3535a1 100644 --- a/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java +++ b/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java @@ -1,5 +1,8 @@ import lombok.SneakyThrows; +import java.util.List; +import java.util.stream.Collectors; + public class HelloThreads { /** * Receives a {@link Runnable} parameter, and returns a {@link Thread}. @@ -60,28 +63,25 @@ public static void runningThreadAndWhenJoinCompleted(Runnable runnable) { thread.join(); } - // accept a list of tasks (Runnable) and return a list of threads that are in progress + /** + * Receives a {@link Runnable} parameter, create three new {@link Thread} start it, + * and return list of threads that are in progress + * @param runnable the code you want to run in new thread and start + * @return list these threads + */ @SneakyThrows - public static void runningMultipleThreadsWithOneTask(Runnable runnable) { + public static List runningMultipleThreadsWithOneTask(Runnable runnable) { var thread1 = new Thread(runnable); var thread2 = new Thread(runnable); var thread3 = new Thread(runnable); - thread1.start(); - thread2.start(); - thread3.start(); - - thread1.join(); - thread2.join(); - thread3.join(); - } - - // accept a thread and make it sleep - @SneakyThrows - public static void runningThreadAndMakeItSleep(Thread thread) { - -// thread.start(); -// Thread.sleep(10000); -// thread.join(); + var threads = List.of(thread1, thread2, thread3) + .stream() + .peek(Thread::start) + .collect(Collectors.toList()); + for (Thread thread : threads) { + thread.join(); + } + return threads; } } From e8d2e2f6cc53e3003aad6f175465d0a0a5e4bde0 Mon Sep 17 00:00:00 2001 From: tkuzub Date: Thu, 7 Oct 2021 10:27:16 +0300 Subject: [PATCH 07/12] GP-106 add new test to 7-0-0-hello-threads --- .../src/test/java/HelloThreadsTest.java | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java b/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java index 03e496ac0..5f0d0e194 100644 --- a/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java +++ b/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java @@ -1,12 +1,12 @@ import lombok.SneakyThrows; import org.junit.jupiter.api.*; -import java.util.List; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ThreadLocalRandom; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; @TestMethodOrder(MethodOrderer.OrderAnnotation.class) class HelloThreadsTest { @@ -17,6 +17,7 @@ class HelloThreadsTest { void prepare() { concurrentLinkedQueue = new ConcurrentLinkedQueue(); } + @SneakyThrows @Test @Order(1) @@ -29,6 +30,7 @@ void runningThread() { checkContentQueue(); } + @SneakyThrows @Test @Order(2) @@ -39,6 +41,7 @@ void runningThreadViaStart() { checkContentQueue(); } + @SneakyThrows @Test @Order(3) @@ -50,6 +53,7 @@ void runningThreadGetNameThread() { checkContentQueue(); } + @SneakyThrows @Test @Order(3) @@ -93,20 +97,9 @@ void runningThreadAndWhenJoinCompleted() { void runningMultipleThreadsWithOneTask() { HelloThreads.runningMultipleThreadsWithOneTask(new Thread(() -> concurrentLinkedQueue.add(ThreadLocalRandom.current().nextInt(100)))); - assertEquals(3, concurrentLinkedQueue.size()); } - @SneakyThrows - @Test - @Order(7) - void runningThreadAndMakeItSleep() { - var thread = new Thread(() -> concurrentLinkedQueue.add(5)); - HelloThreads.runningThreadAndMakeItSleep(thread); - - assertSame(thread.getState(), Thread.State.RUNNABLE); - } - private void checkContentQueue() { assertEquals(1, concurrentLinkedQueue.size()); assertEquals(5, concurrentLinkedQueue.element().intValue()); From e43855f5c661204f5c12ac8962a4bf4c28c5a155 Mon Sep 17 00:00:00 2001 From: tkuzub Date: Thu, 7 Oct 2021 10:41:56 +0300 Subject: [PATCH 08/12] GP-106 add java-fundamentals-util dependency --- 7-0-java-concurrency/7-0-0-hello-threads/pom.xml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/7-0-java-concurrency/7-0-0-hello-threads/pom.xml b/7-0-java-concurrency/7-0-0-hello-threads/pom.xml index 8c17e6eda..4d21d48ea 100644 --- a/7-0-java-concurrency/7-0-0-hello-threads/pom.xml +++ b/7-0-java-concurrency/7-0-0-hello-threads/pom.xml @@ -11,6 +11,14 @@ 4.0.0 7-0-0-hello-theads + + + com.bobocode + java-fundamentals-util + 1.0-SNAPSHOT + compile + + 11 From 48126896ac7ec6589179b4f122c1235ce8db9430 Mon Sep 17 00:00:00 2001 From: tkuzub Date: Thu, 7 Oct 2021 10:43:17 +0300 Subject: [PATCH 09/12] GP-106 prepare the class HelloThreads for realization --- .../src/main/java/HelloThreads.java | 33 +++++-------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java b/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java index 70b3535a1..cec40a8f8 100644 --- a/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java +++ b/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java @@ -1,7 +1,7 @@ +import com.bobocode.util.ExerciseNotCompletedException; import lombok.SneakyThrows; import java.util.List; -import java.util.stream.Collectors; public class HelloThreads { /** @@ -10,7 +10,7 @@ public class HelloThreads { * @return a new thread */ public static Thread runningThread(Runnable runnable) { - return new Thread(runnable); + throw new ExerciseNotCompletedException(); } /** @@ -18,7 +18,7 @@ public static Thread runningThread(Runnable runnable) { * @param thread the code you want to start thread */ public static void runningThreadViaStart(Thread thread) { - thread.start(); + throw new ExerciseNotCompletedException(); } /** @@ -27,8 +27,7 @@ public static void runningThreadViaStart(Thread thread) { * @return the name thread */ public static String runningThreadGetNameThread(Thread thread) { - thread.start(); - return thread.getName(); + throw new ExerciseNotCompletedException(); } /** @@ -37,8 +36,7 @@ public static String runningThreadGetNameThread(Thread thread) { * @return the thread state */ public static Thread.State runningThreadGetStateThread(Thread thread) { - thread.start(); - return thread.getState(); + throw new ExerciseNotCompletedException(); } /** @@ -47,9 +45,7 @@ public static Thread.State runningThreadGetStateThread(Thread thread) { * @return this thread */ public static Thread getSomeLogicRunningTreadAndReturnThisThread(Runnable runnable) { - var thread = new Thread(runnable); - thread.start(); - return thread; + throw new ExerciseNotCompletedException(); } /** @@ -58,9 +54,7 @@ public static Thread getSomeLogicRunningTreadAndReturnThisThread(Runnable runnab */ @SneakyThrows public static void runningThreadAndWhenJoinCompleted(Runnable runnable) { - var thread = new Thread(runnable); - thread.start(); - thread.join(); + throw new ExerciseNotCompletedException(); } /** @@ -71,17 +65,6 @@ public static void runningThreadAndWhenJoinCompleted(Runnable runnable) { */ @SneakyThrows public static List runningMultipleThreadsWithOneTask(Runnable runnable) { - var thread1 = new Thread(runnable); - var thread2 = new Thread(runnable); - var thread3 = new Thread(runnable); - - var threads = List.of(thread1, thread2, thread3) - .stream() - .peek(Thread::start) - .collect(Collectors.toList()); - for (Thread thread : threads) { - thread.join(); - } - return threads; + throw new ExerciseNotCompletedException(); } } From 4fc6d361c8060f406ca3491440788233be854bd0 Mon Sep 17 00:00:00 2001 From: tkuzub Date: 2021εΉ΄10月15ζ—₯ 15:57:22 +0300 Subject: [PATCH 10/12] GP-106 fix after review --- .../7-0-0-hello-threads/pom.xml | 5 +- .../src/main/java/HelloThreads.java | 58 ++++++++++++------- .../src/test/java/HelloThreadsTest.java | 44 +++++++------- 7-0-java-concurrency/pom.xml | 6 +- 4 files changed, 65 insertions(+), 48 deletions(-) diff --git a/7-0-java-concurrency/7-0-0-hello-threads/pom.xml b/7-0-java-concurrency/7-0-0-hello-threads/pom.xml index 4d21d48ea..fec04d7f3 100644 --- a/7-0-java-concurrency/7-0-0-hello-threads/pom.xml +++ b/7-0-java-concurrency/7-0-0-hello-threads/pom.xml @@ -3,14 +3,13 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - java-fundamentals-course + 7-0-java-concurrency com.bobocode 1.0-SNAPSHOT - ../../pom.xml 4.0.0 - 7-0-0-hello-theads + com.bobocode diff --git a/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java b/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java index cec40a8f8..f1387bbad 100644 --- a/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java +++ b/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java @@ -3,68 +3,82 @@ import java.util.List; +import static java.lang.Thread.*; + public class HelloThreads { /** - * Receives a {@link Runnable} parameter, and returns a {@link Thread}. + * Receives a {@link Runnable} parameter that holds the logic and creates a {@link Thread} instance based on it. + * * @param runnable the code you want to run in new thread * @return a new thread */ - public static Thread runningThread(Runnable runnable) { + public static Thread createThread(Runnable runnable) { throw new ExerciseNotCompletedException(); } /** - * Receive a {@link Thread} parameter and start it - * @param thread the code you want to start thread + * Receives a {@link Thread} parameter created based on {@link Runnable}, + * + * @param thread the code you want to run */ - public static void runningThreadViaStart(Thread thread) { + public static void startThread(Thread thread) { throw new ExerciseNotCompletedException(); } /** - * Receives a {@link Thread} parameter, and return a {@link String} - * @param thread the code you want start thread and return tread name + * Receives a {@link Thread} parameter created based on {@link Runnable}, + * and return {@link String} that contains the name this thread + * + * @param thread the code you want run * @return the name thread */ - public static String runningThreadGetNameThread(Thread thread) { + public static String getThreadName(Thread thread) { throw new ExerciseNotCompletedException(); } /** - * Receives a {@link Thread} parameter, and return a {@link Thread.State} - * @param thread the code you want start thread and return state + * Receives a {@link Thread} parameter created based on {@link Runnable}, + * and return a {@link State} that contains the state this thread + * + * @param thread the code you want run * @return the thread state */ - public static Thread.State runningThreadGetStateThread(Thread thread) { + public static State getThreadState(Thread thread) { throw new ExerciseNotCompletedException(); } /** - * Receives a {@link Runnable} parameter, create new {@link Thread}, start and return this {@link Thread} - * @param runnable the code you want to run in new thread and start - * @return this thread + * Receives a {@link Runnable} parameter that holds some logic, create new {@link Thread} instance based on it, + * start and return this instance + * + * @param runnable the code you want to run in new thread + * @return the instance this thread */ - public static Thread getSomeLogicRunningTreadAndReturnThisThread(Runnable runnable) { + public static Thread runInNewThread(Runnable runnable) { throw new ExerciseNotCompletedException(); } /** - * Receives a {@link Runnable} parameter, create new {@link Thread}, start and wait when it has completed - * @param runnable - the code you want to run in new thread and start + * Receives a {@link Runnable} parameter that holds the logic and create new {@link Thread} instance bases on it, + * start and wait when it has completed + * + * @param runnable - the code you want to run in new threads */ @SneakyThrows - public static void runningThreadAndWhenJoinCompleted(Runnable runnable) { + public static void runInNewThreadAndWaitForExecution(Runnable runnable) { throw new ExerciseNotCompletedException(); } /** - * Receives a {@link Runnable} parameter, create three new {@link Thread} start it, + * Receives a {@link Runnable} parameter that holds the logic and creates three {@link Thread} instance based on it * and return list of threads that are in progress - * @param runnable the code you want to run in new thread and start - * @return list these threads + * + * @param runnable the code you want to run in new thread + * @return the list of running threads */ @SneakyThrows - public static List runningMultipleThreadsWithOneTask(Runnable runnable) { + public static List runInMultipleThreads(Runnable runnable) { throw new ExerciseNotCompletedException(); } } + diff --git a/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java b/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java index 5f0d0e194..5ba467c4e 100644 --- a/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java +++ b/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java @@ -14,93 +14,93 @@ class HelloThreadsTest { private Queue concurrentLinkedQueue; @BeforeEach - void prepare() { + void setup() { concurrentLinkedQueue = new ConcurrentLinkedQueue(); } @SneakyThrows @Test @Order(1) - void runningThread() { - Thread runningThread = HelloThreads.runningThread(() -> concurrentLinkedQueue.add(5)); + void createThread() { + Thread runningThread = HelloThreads.createThread(() -> concurrentLinkedQueue.add(5)); assertEquals(0, concurrentLinkedQueue.size()); runningThread.start(); runningThread.join(); - checkContentQueue(); + verifyContentQueue(); } @SneakyThrows @Test @Order(2) - void runningThreadViaStart() { + void startThread() { var thread = new Thread(() -> concurrentLinkedQueue.add(5)); - HelloThreads.runningThreadViaStart(thread); + HelloThreads.startThread(thread); thread.join(); - checkContentQueue(); + verifyContentQueue(); } @SneakyThrows @Test @Order(3) - void runningThreadGetNameThread() { + void getThreadName() { var thread = new Thread(() -> concurrentLinkedQueue.add(5), "name"); - var threadName = HelloThreads.runningThreadGetNameThread(thread); + var threadName = HelloThreads.getThreadName(thread); thread.join(); assertEquals("name", threadName); - checkContentQueue(); + verifyContentQueue(); } @SneakyThrows @Test @Order(3) - void runningThreadGetStateThread() { + void getThreadState() { var thread = new Thread(() -> concurrentLinkedQueue.add(5)); - var state = HelloThreads.runningThreadGetStateThread(thread); + var state = HelloThreads.getThreadState(thread); thread.join(); assertSame(state, Thread.State.RUNNABLE); - checkContentQueue(); + verifyContentQueue(); } @SneakyThrows @Test @Order(4) - void getSomeLogicRunningTreadAndReturnThisThread() { - var thread = HelloThreads.getSomeLogicRunningTreadAndReturnThisThread(new Thread(() -> { + void runInNewThread() { + var thread = HelloThreads.runInNewThread(new Thread(() -> { assertEquals(0, concurrentLinkedQueue.size()); concurrentLinkedQueue.add(5); })); thread.join(); - checkContentQueue(); + verifyContentQueue(); } @SneakyThrows @Test @Order(5) - void runningThreadAndWhenJoinCompleted() { - HelloThreads.runningThreadAndWhenJoinCompleted(new Thread(() -> { + void runInNewThreadAndWaitForExecution() { + HelloThreads.runInNewThreadAndWaitForExecution(new Thread(() -> { assertEquals(0, concurrentLinkedQueue.size()); concurrentLinkedQueue.add(5); })); - checkContentQueue(); + verifyContentQueue(); } @SneakyThrows @Test @Order(6) - void runningMultipleThreadsWithOneTask() { - HelloThreads.runningMultipleThreadsWithOneTask(new Thread(() -> + void runInMultipleThreads() { + HelloThreads.runInMultipleThreads(new Thread(() -> concurrentLinkedQueue.add(ThreadLocalRandom.current().nextInt(100)))); assertEquals(3, concurrentLinkedQueue.size()); } - private void checkContentQueue() { + private void verifyContentQueue() { assertEquals(1, concurrentLinkedQueue.size()); assertEquals(5, concurrentLinkedQueue.element().intValue()); } diff --git a/7-0-java-concurrency/pom.xml b/7-0-java-concurrency/pom.xml index b4437da18..ea713c2f2 100644 --- a/7-0-java-concurrency/pom.xml +++ b/7-0-java-concurrency/pom.xml @@ -2,12 +2,16 @@ + 4.0.0 + pom + + 7-0-0-hello-threads + java-fundamentals-course com.bobocode 1.0-SNAPSHOT - 4.0.0 7-0-java-concurrency From 38d326ea3dfd837878e8ac7ba30d89fe8f8595e1 Mon Sep 17 00:00:00 2001 From: tkuzub Date: 2021εΉ΄11月14ζ—₯ 09:32:02 +0200 Subject: [PATCH 11/12] GP-106 fixed after review --- .../7-0-0-hello-threads/pom.xml | 10 +- .../bobocode/concurrency}/HelloThreads.java | 33 +++--- .../src/test/java/HelloThreadsTest.java | 107 ------------------ .../concurrency/HelloThreadsTest.java | 104 +++++++++++++++++ 7-0-java-concurrency/pom.xml | 9 ++ pom.xml | 3 +- 6 files changed, 133 insertions(+), 133 deletions(-) rename 7-0-java-concurrency/7-0-0-hello-threads/src/main/java/{ => com/bobocode/concurrency}/HelloThreads.java (71%) delete mode 100644 7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java create mode 100644 7-0-java-concurrency/7-0-0-hello-threads/src/test/java/com/bobocode/concurrency/HelloThreadsTest.java diff --git a/7-0-java-concurrency/7-0-0-hello-threads/pom.xml b/7-0-java-concurrency/7-0-0-hello-threads/pom.xml index fec04d7f3..07f1fe6c7 100644 --- a/7-0-java-concurrency/7-0-0-hello-threads/pom.xml +++ b/7-0-java-concurrency/7-0-0-hello-threads/pom.xml @@ -8,16 +8,8 @@ 1.0-SNAPSHOT 4.0.0 - 7-0-0-hello-theads - - - com.bobocode - java-fundamentals-util - 1.0-SNAPSHOT - compile - - + 7-0-0-hello-threads 11 diff --git a/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java b/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/com/bobocode/concurrency/HelloThreads.java similarity index 71% rename from 7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java rename to 7-0-java-concurrency/7-0-0-hello-threads/src/main/java/com/bobocode/concurrency/HelloThreads.java index f1387bbad..2e409ebc0 100644 --- a/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/HelloThreads.java +++ b/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/com/bobocode/concurrency/HelloThreads.java @@ -1,11 +1,23 @@ +package com.bobocode.concurrency; + import com.bobocode.util.ExerciseNotCompletedException; import lombok.SneakyThrows; import java.util.List; -import static java.lang.Thread.*; +import static java.lang.Thread.State; +/** + * {@link HelloThreads} provide a simple API for working with Java threads. A {@link Thread} class represents a key + * concept in Java Concurrency. So this class is meant to teach you the basics like creating a thread or checking its + * state. + *

+ * todo: implement each method according to javadoc and verify your impl by running {@link HelloThreadsTest} + * + * @author Taras Kuzub + */ public class HelloThreads { + /** * Receives a {@link Runnable} parameter that holds the logic and creates a {@link Thread} instance based on it. * @@ -47,17 +59,6 @@ public static State getThreadState(Thread thread) { throw new ExerciseNotCompletedException(); } - /** - * Receives a {@link Runnable} parameter that holds some logic, create new {@link Thread} instance based on it, - * start and return this instance - * - * @param runnable the code you want to run in new thread - * @return the instance this thread - */ - public static Thread runInNewThread(Runnable runnable) { - throw new ExerciseNotCompletedException(); - } - /** * Receives a {@link Runnable} parameter that holds the logic and create new {@link Thread} instance bases on it, * start and wait when it has completed @@ -70,14 +71,16 @@ public static void runInNewThreadAndWaitForExecution(Runnable runnable) { } /** - * Receives a {@link Runnable} parameter that holds the logic and creates three {@link Thread} instance based on it + * Receives three {@link Runnable} parameters that holds the logic and creates three {@link Thread} instances based on it * and return list of threads that are in progress * - * @param runnable the code you want to run in new thread + * @param runnable1 the code you want to run in new thread + * @param runnable2 the code you want to run in new thread + * @param runnable3 the code you want to run in new thread * @return the list of running threads */ @SneakyThrows - public static List runInMultipleThreads(Runnable runnable) { + public static List runInMultipleThreads(Runnable runnable1, Runnable runnable2, Runnable runnable3) { throw new ExerciseNotCompletedException(); } } diff --git a/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java b/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java deleted file mode 100644 index 5ba467c4e..000000000 --- a/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/HelloThreadsTest.java +++ /dev/null @@ -1,107 +0,0 @@ -import lombok.SneakyThrows; -import org.junit.jupiter.api.*; - -import java.util.Queue; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.ThreadLocalRandom; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertSame; - -@TestMethodOrder(MethodOrderer.OrderAnnotation.class) -class HelloThreadsTest { - - private Queue concurrentLinkedQueue; - - @BeforeEach - void setup() { - concurrentLinkedQueue = new ConcurrentLinkedQueue(); - } - - @SneakyThrows - @Test - @Order(1) - void createThread() { - Thread runningThread = HelloThreads.createThread(() -> concurrentLinkedQueue.add(5)); - assertEquals(0, concurrentLinkedQueue.size()); - - runningThread.start(); - runningThread.join(); - - verifyContentQueue(); - } - - @SneakyThrows - @Test - @Order(2) - void startThread() { - var thread = new Thread(() -> concurrentLinkedQueue.add(5)); - HelloThreads.startThread(thread); - thread.join(); - - verifyContentQueue(); - } - - @SneakyThrows - @Test - @Order(3) - void getThreadName() { - var thread = new Thread(() -> concurrentLinkedQueue.add(5), "name"); - var threadName = HelloThreads.getThreadName(thread); - thread.join(); - assertEquals("name", threadName); - - verifyContentQueue(); - } - - @SneakyThrows - @Test - @Order(3) - void getThreadState() { - var thread = new Thread(() -> concurrentLinkedQueue.add(5)); - var state = HelloThreads.getThreadState(thread); - thread.join(); - assertSame(state, Thread.State.RUNNABLE); - - verifyContentQueue(); - } - - @SneakyThrows - @Test - @Order(4) - void runInNewThread() { - var thread = HelloThreads.runInNewThread(new Thread(() -> { - assertEquals(0, concurrentLinkedQueue.size()); - concurrentLinkedQueue.add(5); - })); - thread.join(); - - verifyContentQueue(); - } - - @SneakyThrows - @Test - @Order(5) - void runInNewThreadAndWaitForExecution() { - HelloThreads.runInNewThreadAndWaitForExecution(new Thread(() -> { - assertEquals(0, concurrentLinkedQueue.size()); - concurrentLinkedQueue.add(5); - })); - - verifyContentQueue(); - } - - @SneakyThrows - @Test - @Order(6) - void runInMultipleThreads() { - HelloThreads.runInMultipleThreads(new Thread(() -> - concurrentLinkedQueue.add(ThreadLocalRandom.current().nextInt(100)))); - assertEquals(3, concurrentLinkedQueue.size()); - } - - private void verifyContentQueue() { - assertEquals(1, concurrentLinkedQueue.size()); - assertEquals(5, concurrentLinkedQueue.element().intValue()); - } -} \ No newline at end of file diff --git a/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/com/bobocode/concurrency/HelloThreadsTest.java b/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/com/bobocode/concurrency/HelloThreadsTest.java new file mode 100644 index 000000000..f2d12fb2d --- /dev/null +++ b/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/com/bobocode/concurrency/HelloThreadsTest.java @@ -0,0 +1,104 @@ +package com.bobocode.concurrency; + +import lombok.SneakyThrows; +import org.junit.jupiter.api.*; + +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ThreadLocalRandom; + +import static java.lang.Thread.State.RUNNABLE; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; + +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class HelloThreadsTest { + + private Queue concurrentLinkedQueue; + + @BeforeEach + void setup() { + concurrentLinkedQueue = new ConcurrentLinkedQueue(); + } + + @SneakyThrows + @Test + @Order(1) + void createThread() { + var thread = HelloThreads.createThread(() -> concurrentLinkedQueue.add(5)); + + thread.start(); + + assertAll( + () -> assertThat(concurrentLinkedQueue).hasSize(1), + () -> assertThat(concurrentLinkedQueue.element().intValue()).isEqualTo(5) + ); + } + + @SneakyThrows + @Test + @Order(2) + void startThread() { + var thread = new Thread(() -> concurrentLinkedQueue.add(5)); + + HelloThreads.startThread(thread); + + assertAll( + () -> assertThat(concurrentLinkedQueue).hasSize(1), + () -> assertThat(concurrentLinkedQueue.element().intValue()).isEqualTo(5) + ); + } + + @SneakyThrows + @Test + @Order(3) + void getThreadName() { + var thread = new Thread(() -> concurrentLinkedQueue.add(5), "Hello Thread"); + + var name = HelloThreads.getThreadName(thread); + + assertThat(name).isEqualTo("Hello Thread"); + } + + @SneakyThrows + @Test + @Order(4) + void getThreadState() { + var thread = new Thread(() -> concurrentLinkedQueue.add(5)); + + var state = HelloThreads.getThreadState(thread); + + assertThat(state).isEqualTo(RUNNABLE); + } + + @SneakyThrows + @Test + @Order(5) + void runInNewThreadAndWaitForExecution() { + HelloThreads.runInNewThreadAndWaitForExecution(new Thread(() -> { + concurrentLinkedQueue.add(5); + assertThat(concurrentLinkedQueue).hasSize(0); + })); + + assertAll( + () -> assertThat(concurrentLinkedQueue).hasSize(1), + () -> assertThat(concurrentLinkedQueue.element().intValue()).isEqualTo(5) + ); + } + + @SneakyThrows + @Test + @Order(6) + void runInMultipleThreads() { + var threads = HelloThreads.runInMultipleThreads( + new Thread(() -> concurrentLinkedQueue.add(ThreadLocalRandom.current().nextInt(100))), + new Thread(() -> concurrentLinkedQueue.add(ThreadLocalRandom.current().nextInt(100))), + new Thread(() -> concurrentLinkedQueue.add(ThreadLocalRandom.current().nextInt(100))) + ); + + assertAll( + () -> assertThat(concurrentLinkedQueue).hasSize(3), + () -> assertThat(threads).hasSize(3) + ); + } +} \ No newline at end of file diff --git a/7-0-java-concurrency/pom.xml b/7-0-java-concurrency/pom.xml index ea713c2f2..21d3d661e 100644 --- a/7-0-java-concurrency/pom.xml +++ b/7-0-java-concurrency/pom.xml @@ -7,6 +7,7 @@ 7-0-0-hello-threads + java-fundamentals-course com.bobocode @@ -18,4 +19,12 @@ 11 11 + + + + com.bobocode + java-fundamentals-util + 1.0-SNAPSHOT + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 031798b9d..3198b50c5 100644 --- a/pom.xml +++ b/pom.xml @@ -23,10 +23,9 @@ 4-0-object-oriented-programming 5-0-functional-programming 6-0-test-driven-development + 7-0-java-concurrency java-fundamentals-util lesson-demo - 7-0-java-concurrency - 7-0-java-concurrency/7-0-0-hello-threads From c0f75686d1022886be864484a33dff0e15acc3b0 Mon Sep 17 00:00:00 2001 From: tkuzub Date: Fri, 3 Dec 2021 20:30:37 +0200 Subject: [PATCH 12/12] GP-106 changed the tests for some tasks --- .../bobocode/concurrency/HelloThreads.java | 22 ++++++----- .../concurrency/HelloThreadsTest.java | 37 ++++++++++--------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/com/bobocode/concurrency/HelloThreads.java b/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/com/bobocode/concurrency/HelloThreads.java index 2e409ebc0..10558fc0a 100644 --- a/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/com/bobocode/concurrency/HelloThreads.java +++ b/7-0-java-concurrency/7-0-0-hello-threads/src/main/java/com/bobocode/concurrency/HelloThreads.java @@ -60,27 +60,29 @@ public static State getThreadState(Thread thread) { } /** - * Receives a {@link Runnable} parameter that holds the logic and create new {@link Thread} instance bases on it, - * start and wait when it has completed + * Receives a {@link Thread} parameter created based on {@link Runnable}, + * start and wait when thread completed * - * @param runnable - the code you want to run in new threads + * @param thread the code you want run */ @SneakyThrows - public static void runInNewThreadAndWaitForExecution(Runnable runnable) { + public static void waitForThreadToComplete(Thread thread) { throw new ExerciseNotCompletedException(); } /** - * Receives three {@link Runnable} parameters that holds the logic and creates three {@link Thread} instances based on it - * and return list of threads that are in progress + * Receives three {@link Thread} parameters created based on {@link Runnable} + * start all threads and wait when all threads completed. + * You should return {@link List} of threads that have already been completed + * + * @param thread1 the code you want run + * @param thread2 the code you want run + * @param thread3 the code you want run * - * @param runnable1 the code you want to run in new thread - * @param runnable2 the code you want to run in new thread - * @param runnable3 the code you want to run in new thread * @return the list of running threads */ @SneakyThrows - public static List runInMultipleThreads(Runnable runnable1, Runnable runnable2, Runnable runnable3) { + public static List getListThreads(Thread thread1, Thread thread2, Thread thread3) { throw new ExerciseNotCompletedException(); } } diff --git a/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/com/bobocode/concurrency/HelloThreadsTest.java b/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/com/bobocode/concurrency/HelloThreadsTest.java index f2d12fb2d..21ec2241b 100644 --- a/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/com/bobocode/concurrency/HelloThreadsTest.java +++ b/7-0-java-concurrency/7-0-0-hello-threads/src/test/java/com/bobocode/concurrency/HelloThreadsTest.java @@ -5,11 +5,11 @@ import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.ThreadLocalRandom; import static java.lang.Thread.State.RUNNABLE; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertAll; +import static org.mockito.Mockito.*; @TestMethodOrder(MethodOrderer.OrderAnnotation.class) class HelloThreadsTest { @@ -54,7 +54,6 @@ void startThread() { @Order(3) void getThreadName() { var thread = new Thread(() -> concurrentLinkedQueue.add(5), "Hello Thread"); - var name = HelloThreads.getThreadName(thread); assertThat(name).isEqualTo("Hello Thread"); @@ -74,31 +73,35 @@ void getThreadState() { @SneakyThrows @Test @Order(5) - void runInNewThreadAndWaitForExecution() { - HelloThreads.runInNewThreadAndWaitForExecution(new Thread(() -> { - concurrentLinkedQueue.add(5); - assertThat(concurrentLinkedQueue).hasSize(0); - })); + void waitForThreadToComplete() { + var thread = mock(Thread.class); + + HelloThreads.waitForThreadToComplete(thread); assertAll( - () -> assertThat(concurrentLinkedQueue).hasSize(1), - () -> assertThat(concurrentLinkedQueue.element().intValue()).isEqualTo(5) + () -> verify(thread, times(1)).start(), + () -> verify(thread, times(1)).join() ); } @SneakyThrows @Test @Order(6) - void runInMultipleThreads() { - var threads = HelloThreads.runInMultipleThreads( - new Thread(() -> concurrentLinkedQueue.add(ThreadLocalRandom.current().nextInt(100))), - new Thread(() -> concurrentLinkedQueue.add(ThreadLocalRandom.current().nextInt(100))), - new Thread(() -> concurrentLinkedQueue.add(ThreadLocalRandom.current().nextInt(100))) - ); + void getListThreads() { + var thread1 = mock(Thread.class); + var thread2 = mock(Thread.class); + var thread3 = mock(Thread.class); + + var threads = HelloThreads.getListThreads(thread1, thread2, thread3); assertAll( - () -> assertThat(concurrentLinkedQueue).hasSize(3), - () -> assertThat(threads).hasSize(3) + () -> assertThat(threads).hasSize(3), + () -> verify(thread1, times(1)).start(), + () -> verify(thread1, times(1)).join(), + () -> verify(thread2, times(1)).start(), + () -> verify(thread2, times(1)).join(), + () -> verify(thread3, times(1)).start(), + () -> verify(thread3, times(1)).join() ); } } \ No newline at end of file

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /