File tree Expand file tree Collapse file tree
main/kotlin/com/baeldung/thread
test/kotlin/com/baeldung/thread Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com.baeldung.thread
2+
3+ class SimpleRunnable : Runnable {
4+
5+ override fun run () {
6+ println (" ${Thread .currentThread()} has run." )
7+ }
8+ }
Original file line number Diff line number Diff line change 1+ package com.baeldung.thread
2+
3+ class SimpleThread : Thread () {
4+
5+ override fun run () {
6+ println (" ${Thread .currentThread()} has run." )
7+ }
8+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments