-
-
Notifications
You must be signed in to change notification settings - Fork 467
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
106 hello thread exercise demo #109
Changes from all commits
fbe135a
2a18af9
ff7238b
9f5450e
234e63a
41c3d07
e8d2e2f
e43855f
4812689
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?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>java-fundamentals-course</artifactId> | ||
<groupId>com.bobocode</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>7-0-0-hello-theads</artifactId> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.bobocode</groupId> | ||
<artifactId>java-fundamentals-util</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<properties> | ||
<maven.compile.targer>11</maven.compile.targer> | ||
<maven.compile.source>11</maven.compile.source> | ||
</properties> | ||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import com.bobocode.util.ExerciseNotCompletedException; | ||
import lombok.SneakyThrows; | ||
|
||
import java.util.List; | ||
|
||
public class HelloThreads { | ||
/** | ||
* Receives a {@link Runnable} parameter, and returns a {@link Thread}. | ||
* @param runnable the code you want to run in new thread | ||
* @return a new thread | ||
*/ | ||
public static Thread runningThread(Runnable runnable) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tkuzub let's use verbs in method names. E.g. |
||
throw new ExerciseNotCompletedException(); | ||
} | ||
|
||
/** | ||
* Receive a {@link Thread} parameter and start it | ||
* @param thread the code you want to start thread | ||
*/ | ||
public static void runningThreadViaStart(Thread thread) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tkuzub this one should be called |
||
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 | ||
* @return the name thread | ||
*/ | ||
public static String runningThreadGetNameThread(Thread thread) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tkuzub this one should be called |
||
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 | ||
* @return the thread state | ||
*/ | ||
public static Thread.State runningThreadGetStateThread(Thread thread) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tkuzub please use static import for class |
||
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 | ||
*/ | ||
public static Thread getSomeLogicRunningTreadAndReturnThisThread(Runnable runnable) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tkuzub for instance this one can be called |
||
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 | ||
*/ | ||
@SneakyThrows | ||
public static void runningThreadAndWhenJoinCompleted(Runnable runnable) { | ||
throw new ExerciseNotCompletedException(); | ||
} | ||
|
||
/** | ||
* 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 List<Thread> runningMultipleThreadsWithOneTask(Runnable runnable) { | ||
throw new ExerciseNotCompletedException(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
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<Integer> concurrentLinkedQueue; | ||
|
||
@BeforeEach | ||
void prepare() { | ||
concurrentLinkedQueue = new ConcurrentLinkedQueue<>(); | ||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
@Order(1) | ||
void runningThread() { | ||
Thread runningThread = HelloThreads.runningThread(() -> concurrentLinkedQueue.add(5)); | ||
assertEquals(0, concurrentLinkedQueue.size()); | ||
|
||
runningThread.start(); | ||
runningThread.join(); | ||
|
||
checkContentQueue(); | ||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
@Order(2) | ||
void runningThreadViaStart() { | ||
var thread = new Thread(() -> concurrentLinkedQueue.add(5)); | ||
HelloThreads.runningThreadViaStart(thread); | ||
thread.join(); | ||
|
||
checkContentQueue(); | ||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
@Order(3) | ||
void runningThreadGetNameThread() { | ||
var thread = new Thread(() -> concurrentLinkedQueue.add(5), "name"); | ||
var threadName = HelloThreads.runningThreadGetNameThread(thread); | ||
thread.join(); | ||
assertEquals("name", threadName); | ||
|
||
checkContentQueue(); | ||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
@Order(3) | ||
void runningThreadGetStateThread() { | ||
var thread = new Thread(() -> concurrentLinkedQueue.add(5)); | ||
var state = HelloThreads.runningThreadGetStateThread(thread); | ||
thread.join(); | ||
assertSame(state, Thread.State.RUNNABLE); | ||
|
||
checkContentQueue(); | ||
} | ||
|
||
@SneakyThrows | ||
@Test | ||
@Order(4) | ||
void getSomeLogicRunningTreadAndReturnThisThread() { | ||
var thread = HelloThreads.getSomeLogicRunningTreadAndReturnThisThread(new Thread(() -> { | ||
assertEquals(0, concurrentLinkedQueue.size()); | ||
concurrentLinkedQueue.add(5); | ||
})); | ||
thread.join(); | ||
|
||
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()); | ||
} | ||
|
||
private void checkContentQueue() { | ||
assertEquals(1, concurrentLinkedQueue.size()); | ||
assertEquals(5, concurrentLinkedQueue.element().intValue()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?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>java-fundamentals-course</artifactId> | ||
<groupId>com.bobocode</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>7-0-java-concurrency</artifactId> | ||
<properties> | ||
<maven.compile.targer>11</maven.compile.targer> | ||
<maven.compile.source>11</maven.compile.source> | ||
</properties> | ||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,8 @@ | |
<module>6-0-test-driven-development</module> | ||
<module>java-fundamentals-util</module> | ||
<module>lesson-demo</module> | ||
<module>7-0-java-concurrency</module> | ||
<module>7-0-java-concurrency/7-0-0-hello-threads</module> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tkuzub as you can see, this pom file should hold only high-level modules. If you want to add a nested module, |
||
</modules> | ||
|
||
<dependencies> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tkuzub Javadoc should provide more details. In this case, it should say