Skip to content

Commit 2d3acce

Browse files
committed
Thread and Coroutines in Kotlin
1 parent acf37e5 commit 2d3acce

4 files changed

Lines changed: 122 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.thread
2+
3+
class SimpleRunnable: Runnable {
4+
5+
override fun run() {
6+
println("${Thread.currentThread()} has run.")
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.thread
2+
3+
class SimpleThread: Thread() {
4+
5+
override fun run() {
6+
println("${Thread.currentThread()} has run.")
7+
}
8+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.thread
2+
3+
import kotlinx.coroutines.experimental.*
4+
import org.junit.jupiter.api.Test
5+
6+
class CoroutineUnitTest {
7+
8+
@Test
9+
fun whenCreateLaunchCoroutineWithoutContext_thenRun() {
10+
11+
val job = launch {
12+
println("${Thread.currentThread()} has run.")
13+
}
14+
15+
runBlocking {
16+
job.join()
17+
}
18+
}
19+
20+
@Test
21+
fun whenCreateLaunchCoroutineWithDefaultContext_thenRun() {
22+
23+
val job = launch(DefaultDispatcher) {
24+
println("${Thread.currentThread()} has run.")
25+
}
26+
27+
runBlocking {
28+
job.join()
29+
}
30+
}
31+
32+
@Test
33+
fun whenCreateLaunchCoroutineWithUnconfinedContext_thenRun() {
34+
35+
val job = launch(Unconfined) {
36+
println("${Thread.currentThread()} has run.")
37+
}
38+
39+
runBlocking {
40+
job.join()
41+
}
42+
}
43+
44+
@Test
45+
fun whenCreateLaunchCoroutineWithDedicatedThread_thenRun() {
46+
47+
val job = launch(newSingleThreadContext("dedicatedThread")) {
48+
println("${Thread.currentThread()} has run.")
49+
}
50+
51+
runBlocking {
52+
job.join()
53+
}
54+
}
55+
56+
@Test
57+
fun whenCreateAsyncCoroutine_thenRun() {
58+
59+
val deferred = async(Unconfined) {
60+
return@async "${Thread.currentThread()} has run."
61+
}
62+
63+
runBlocking {
64+
val result = deferred.await()
65+
println(result)
66+
}
67+
}
68+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.thread
2+
3+
import org.junit.jupiter.api.Test
4+
import kotlin.concurrent.thread
5+
6+
class ThreadUnitTest {
7+
8+
@Test
9+
fun whenCreateThread_thenRun() {
10+
11+
val thread = SimpleThread()
12+
thread.start()
13+
}
14+
15+
@Test
16+
fun whenCreateThreadWithRunnable_thenRun() {
17+
18+
val threadWithRunnable = Thread(SimpleRunnable())
19+
threadWithRunnable.start()
20+
}
21+
22+
@Test
23+
fun whenCreateThreadWithSAMConversions_thenRun() {
24+
25+
val thread = Thread {
26+
println("${Thread.currentThread()} has run.")
27+
}
28+
thread.start()
29+
}
30+
31+
@Test
32+
fun whenCreateThreadWithMethodExtension_thenRun() {
33+
34+
thread(start = true) {
35+
println("${Thread.currentThread()} has run.")
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)