diff --git a/core-kotlin/src/main/kotlin/com/baeldung/thread/SimpleRunnable.kt b/core-kotlin/src/main/kotlin/com/baeldung/thread/SimpleRunnable.kt new file mode 100644 index 0000000000..7bc0528d06 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/thread/SimpleRunnable.kt @@ -0,0 +1,8 @@ +package com.baeldung.thread + +class SimpleRunnable: Runnable { + + override fun run() { + println("${Thread.currentThread()} has run.") + } +} \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/thread/SimpleThread.kt b/core-kotlin/src/main/kotlin/com/baeldung/thread/SimpleThread.kt new file mode 100644 index 0000000000..2b2827ae02 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/thread/SimpleThread.kt @@ -0,0 +1,8 @@ +package com.baeldung.thread + +class SimpleThread: Thread() { + + override fun run() { + println("${Thread.currentThread()} has run.") + } +} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/thread/CoroutineUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/thread/CoroutineUnitTest.kt new file mode 100644 index 0000000000..215fa6710f --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/thread/CoroutineUnitTest.kt @@ -0,0 +1,68 @@ +package com.baeldung.thread + +import kotlinx.coroutines.experimental.* +import org.junit.jupiter.api.Test + +class CoroutineUnitTest { + + @Test + fun whenCreateLaunchCoroutineWithoutContext_thenRun() { + + val job = launch { + println("${Thread.currentThread()} has run.") + } + + runBlocking { + job.join() + } + } + + @Test + fun whenCreateLaunchCoroutineWithDefaultContext_thenRun() { + + val job = launch(DefaultDispatcher) { + println("${Thread.currentThread()} has run.") + } + + runBlocking { + job.join() + } + } + + @Test + fun whenCreateLaunchCoroutineWithUnconfinedContext_thenRun() { + + val job = launch(Unconfined) { + println("${Thread.currentThread()} has run.") + } + + runBlocking { + job.join() + } + } + + @Test + fun whenCreateLaunchCoroutineWithDedicatedThread_thenRun() { + + val job = launch(newSingleThreadContext("dedicatedThread")) { + println("${Thread.currentThread()} has run.") + } + + runBlocking { + job.join() + } + } + + @Test + fun whenCreateAsyncCoroutine_thenRun() { + + val deferred = async(Unconfined) { + return@async "${Thread.currentThread()} has run." + } + + runBlocking { + val result = deferred.await() + println(result) + } + } +} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/thread/ThreadUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/thread/ThreadUnitTest.kt new file mode 100644 index 0000000000..fa2f1edc36 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/thread/ThreadUnitTest.kt @@ -0,0 +1,38 @@ +package com.baeldung.thread + +import org.junit.jupiter.api.Test +import kotlin.concurrent.thread + +class ThreadUnitTest { + + @Test + fun whenCreateThread_thenRun() { + + val thread = SimpleThread() + thread.start() + } + + @Test + fun whenCreateThreadWithRunnable_thenRun() { + + val threadWithRunnable = Thread(SimpleRunnable()) + threadWithRunnable.start() + } + + @Test + fun whenCreateThreadWithSAMConversions_thenRun() { + + val thread = Thread { + println("${Thread.currentThread()} has run.") + } + thread.start() + } + + @Test + fun whenCreateThreadWithMethodExtension_thenRun() { + + thread(start = true) { + println("${Thread.currentThread()} has run.") + } + } +} \ No newline at end of file