Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
ghost wants to merge 12 commits into main from 106-hello-thread-exercise-demo
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
12 commits
Select commit Hold shift + click to select a range
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
View file Open in desktop
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
View file Open in desktop
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>
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();
}
}

View file Open in desktop
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
View file Open in desktop
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>
1 change: 1 addition & 0 deletions pom.xml
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<module>4-0-object-oriented-programming</module>
<module>5-0-functional-programming</module>
<module>6-0-test-driven-development</module>
<module>7-0-java-concurrency</module>
<module>java-fundamentals-util</module>
<module>lesson-demo</module>
</modules>
Expand Down

AltStyle によって変換されたページ (->オリジナル) /