Skip to content
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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions 7-0-java-concurrency/7-0-0-hello-threads/README.md
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)
27 changes: 27 additions & 0 deletions 7-0-java-concurrency/7-0-0-hello-threads/pom.xml
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}.
Copy link
Contributor

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

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tkuzub let's use verbs in method names. E.g. createThread(Runnable runnable)

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tkuzub this one should be called startThread

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tkuzub this one should be called getThreadName

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tkuzub please use static import for class State

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tkuzub for instance this one can be called runInNewThread

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());
}
}
17 changes: 17 additions & 0 deletions 7-0-java-concurrency/pom.xml
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>
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Copy link
Contributor

Choose a reason for hiding this comment

The 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, 7-0-0-hello-threads, you should specify it in 7-0-java-concurrency/pom.xml

</modules>

<dependencies>
Expand Down