-
-
Notifications
You must be signed in to change notification settings - Fork 501
106 hello thread exercise demo #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fbe135a
GP-106 Add README.md
tkuzub 2a18af9
GP-106 Add pom and module concurrency main pom
tkuzub ff7238b
GP-106 Add new task for 7-0-0-hello-threads
tkuzub 9f5450e
GP-106 add test for 7-0-0-hello-threads
tkuzub 234e63a
GP-106 add solution for 7-0-0-hello-thread
tkuzub 41c3d07
GP-106 add new task to 7-0-0-hello-threads
tkuzub e8d2e2f
GP-106 add new test to 7-0-0-hello-threads
tkuzub e43855f
GP-106 add java-fundamentals-util dependency
tkuzub 4812689
GP-106 prepare the class HelloThreads for realization
tkuzub 4fc6d36
GP-106 fix after review
tkuzub 38d326e
GP-106 fixed after review
tkuzub c0f7568
GP-106 changed the tests for some tasks
tkuzub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7-0-java-concurrency/7-0-0-hello-threads/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>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) |
18 changes: 18 additions & 0 deletions
7-0-java-concurrency/7-0-0-hello-threads/pom.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
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"> | ||
<parent> | ||
<artifactId>7-0-java-concurrency</artifactId> | ||
<groupId>com.bobocode</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>7-0-0-hello-threads</artifactId> | ||
|
||
<properties> | ||
<maven.compile.targer>11</maven.compile.targer> | ||
<maven.compile.source>11</maven.compile.source> | ||
</properties> | ||
</project> |
89 changes: 89 additions & 0 deletions
...-concurrency/7-0-0-hello-threads/src/main/java/com/bobocode/concurrency/HelloThreads.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.bobocode.concurrency; | ||
|
||
import com.bobocode.util.ExerciseNotCompletedException; | ||
import lombok.SneakyThrows; | ||
|
||
import java.util.List; | ||
|
||
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. | ||
* <p> | ||
* 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. | ||
* | ||
* @param runnable the code you want to run in new thread | ||
* @return a new thread | ||
*/ | ||
public static Thread createThread(Runnable runnable) { | ||
throw new ExerciseNotCompletedException(); | ||
} | ||
|
||
/** | ||
* Receives a {@link Thread} parameter created based on {@link Runnable}, | ||
* | ||
* @param thread the code you want to run | ||
*/ | ||
public static void startThread(Thread thread) { | ||
throw new ExerciseNotCompletedException(); | ||
} | ||
|
||
/** | ||
* 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 getThreadName(Thread thread) { | ||
throw new ExerciseNotCompletedException(); | ||
} | ||
|
||
/** | ||
* 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 State getThreadState(Thread thread) { | ||
throw new ExerciseNotCompletedException(); | ||
} | ||
|
||
/** | ||
* Receives a {@link Thread} parameter created based on {@link Runnable}, | ||
* start and wait when thread completed | ||
* | ||
* @param thread the code you want run | ||
*/ | ||
@SneakyThrows | ||
public static void waitForThreadToComplete(Thread thread) { | ||
throw new ExerciseNotCompletedException(); | ||
} | ||
|
||
/** | ||
* 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 | ||
* | ||
* @return the list of running threads | ||
*/ | ||
@SneakyThrows | ||
public static List<Thread> getListThreads(Thread thread1, Thread thread2, Thread thread3) { | ||
throw new ExerciseNotCompletedException(); | ||
} | ||
} | ||
|
107 changes: 107 additions & 0 deletions
...currency/7-0-0-hello-threads/src/test/java/com/bobocode/concurrency/HelloThreadsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.bobocode.concurrency; | ||
|
||
import lombok.SneakyThrows; | ||
import org.junit.jupiter.api.*; | ||
|
||
import java.util.Queue; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
|
||
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 { | ||
|
||
private Queue<Integer> 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 waitForThreadToComplete() { | ||
var thread = mock(Thread.class); | ||
|
||
HelloThreads.waitForThreadToComplete(thread); | ||
|
||
assertAll( | ||
() -> verify(thread, times(1)).start(), | ||
() -> verify(thread, times(1)).join() | ||
); | ||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
@Order(6) | ||
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(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() | ||
); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
7-0-java-concurrency/pom.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>pom</packaging> | ||
<modules> | ||
<module>7-0-0-hello-threads</module> | ||
</modules> | ||
|
||
<parent> | ||
<artifactId>java-fundamentals-course</artifactId> | ||
<groupId>com.bobocode</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>7-0-java-concurrency</artifactId> | ||
<properties> | ||
<maven.compile.targer>11</maven.compile.targer> | ||
<maven.compile.source>11</maven.compile.source> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.bobocode</groupId> | ||
<artifactId>java-fundamentals-util</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.