Skip to content

Commit 19216b0

Browse files
feat: Create simple thread using Java thread class (#216)
* Create simple thread using Java thread class * Fixed checkstyle issues
1 parent 0cdf57c commit 19216b0

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,6 +2016,23 @@ public class StringToDateSnippet {
20162016

20172017
### Thread Pool
20182018

2019+
### Thread
2020+
2021+
```java
2022+
public class ThreadSnippet {
2023+
2024+
/**
2025+
* Creates and returns a new thread with the task assigned to it (task will be performed parallel to the main thread).
2026+
*
2027+
* @param task the task to be executed by this thread
2028+
* @return new thread with task assigned to it.
2029+
*/
2030+
public static Thread createThread(Runnable task) {
2031+
return new Thread(task);
2032+
}
2033+
}
2034+
```
2035+
20192036
```java
20202037
public class ThreadPool {
20212038

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2017-2022 Ilkka Seppälä
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package thread;
26+
27+
/**
28+
* ThreadSnippet.
29+
*/
30+
public class ThreadSnippet {
31+
32+
/**
33+
* Creates and returns a new thread with the task assigned to it
34+
* (task will be performed parallel to the main thread).
35+
*
36+
* @param task the task to be executed by this thread
37+
* @return new thread with task assigned to it.
38+
*/
39+
public static Thread createThread(Runnable task) {
40+
return new Thread(task);
41+
}
42+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2017-2022 Ilkka Seppälä
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package thread;
26+
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
29+
import java.util.concurrent.atomic.AtomicInteger;
30+
import org.junit.jupiter.api.Test;
31+
32+
33+
/*
34+
* Tests for 30 Seconds of Java code library
35+
*
36+
*/
37+
class ThreadSnippetTest {
38+
39+
/**
40+
* Tests for {@link ThreadSnippet#createThread(Runnable)}.
41+
*/
42+
@Test
43+
void createThread() throws InterruptedException {
44+
AtomicInteger counter = new AtomicInteger(0);
45+
46+
Thread t = ThreadSnippet.createThread(() -> {
47+
for (int i = 0; i < 1000000; i++) {
48+
counter.getAndIncrement();
49+
}
50+
});
51+
52+
t.start();
53+
t.join();
54+
55+
assertEquals(counter.get(), 1000000);
56+
}
57+
}

0 commit comments

Comments
 (0)