Merge pull request #5395 from freddyaott/master
[BAEL-2264] Creating a Thread in Kotlin
This commit is contained in:
commit
55ccc6b433
|
@ -0,0 +1,8 @@
|
||||||
|
package com.baeldung.thread
|
||||||
|
|
||||||
|
class SimpleRunnable: Runnable {
|
||||||
|
|
||||||
|
override fun run() {
|
||||||
|
println("${Thread.currentThread()} has run.")
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package com.baeldung.thread
|
||||||
|
|
||||||
|
class SimpleThread: Thread() {
|
||||||
|
|
||||||
|
override fun run() {
|
||||||
|
println("${Thread.currentThread()} has run.")
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,14 +24,15 @@ internal class SliceTest {
|
||||||
assertIterableEquals(expected, actual)
|
assertIterableEquals(expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
// From the 1.3 version of Kotlin APIs, slice doesn't return array of nulls but throw IndexOutOfBoundsException
|
||||||
fun whenSlicingBeyondTheRangeOfTheArray_thenContainManyNulls() {
|
// @Test
|
||||||
val original = arrayOf(12, 3, 34, 4)
|
// fun whenSlicingBeyondTheRangeOfTheArray_thenContainManyNulls() {
|
||||||
val actual = original.slice(3..8)
|
// val original = arrayOf(12, 3, 34, 4)
|
||||||
val expected = listOf(4, null, null, null, null, null)
|
// val actual = original.slice(3..8)
|
||||||
|
// val expected = listOf(4, null, null, null, null, null)
|
||||||
assertIterableEquals(expected, actual)
|
//
|
||||||
}
|
// assertIterableEquals(expected, actual)
|
||||||
|
// }
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun whenSlicingBeyondRangeOfArrayWithStep_thenOutOfBoundsException() {
|
fun whenSlicingBeyondRangeOfArrayWithStep_thenOutOfBoundsException() {
|
||||||
|
|
|
@ -1,16 +1,12 @@
|
||||||
package com.baeldung.fuel
|
package com.baeldung.fuel
|
||||||
|
|
||||||
import awaitObjectResult
|
|
||||||
import awaitStringResponse
|
|
||||||
import com.github.kittinunf.fuel.Fuel
|
import com.github.kittinunf.fuel.Fuel
|
||||||
import com.github.kittinunf.fuel.core.FuelManager
|
import com.github.kittinunf.fuel.core.FuelManager
|
||||||
import com.github.kittinunf.fuel.core.Request
|
|
||||||
import com.github.kittinunf.fuel.core.interceptors.cUrlLoggingRequestInterceptor
|
import com.github.kittinunf.fuel.core.interceptors.cUrlLoggingRequestInterceptor
|
||||||
import com.github.kittinunf.fuel.gson.responseObject
|
import com.github.kittinunf.fuel.gson.responseObject
|
||||||
import com.github.kittinunf.fuel.httpGet
|
import com.github.kittinunf.fuel.httpGet
|
||||||
import com.github.kittinunf.fuel.rx.rx_object
|
import com.github.kittinunf.fuel.rx.rx_object
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import kotlinx.coroutines.experimental.runBlocking
|
|
||||||
import org.junit.jupiter.api.Assertions
|
import org.junit.jupiter.api.Assertions
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
@ -226,32 +222,26 @@ internal class FuelHttpUnitTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
fun whenMakeGETRequestUsingCoroutines_thenResponseStatusCode200() {
|
|
||||||
|
|
||||||
runBlocking {
|
// The new 1.3 coroutine APIs, aren't implemented yet in Fuel Library
|
||||||
val (request, response, result) = Fuel.get("http://httpbin.org/get").awaitStringResponse()
|
// @Test
|
||||||
|
// fun whenMakeGETRequestUsingCoroutines_thenResponseStatusCode200() = runBlocking {
|
||||||
|
// val (request, response, result) = Fuel.get("http://httpbin.org/get").awaitStringResponse()
|
||||||
|
//
|
||||||
|
// result.fold({ data ->
|
||||||
|
// Assertions.assertEquals(200, response.statusCode)
|
||||||
|
//
|
||||||
|
// }, { error -> })
|
||||||
|
// }
|
||||||
|
|
||||||
result.fold({ data ->
|
// The new 1.3 coroutine APIs, aren't implemented yet in Fuel Library
|
||||||
Assertions.assertEquals(200, response.statusCode)
|
// @Test
|
||||||
|
// fun whenMakeGETRequestUsingCoroutines_thenDeserializeResponse() = runBlocking {
|
||||||
}, { error -> })
|
// Fuel.get("https://jsonplaceholder.typicode.com/posts?id=1").awaitObjectResult(Post.Deserializer())
|
||||||
}
|
// .fold({ data ->
|
||||||
|
// Assertions.assertEquals(1, data.get(0).userId)
|
||||||
}
|
// }, { error -> })
|
||||||
|
// }
|
||||||
@Test
|
|
||||||
fun whenMakeGETRequestUsingCoroutines_thenDeserializeResponse() {
|
|
||||||
|
|
||||||
|
|
||||||
runBlocking {
|
|
||||||
Fuel.get("https://jsonplaceholder.typicode.com/posts?id=1").awaitObjectResult(Post.Deserializer())
|
|
||||||
.fold({ data ->
|
|
||||||
Assertions.assertEquals(1, data.get(0).userId)
|
|
||||||
}, { error -> })
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun whenMakeGETPostRequestUsingRoutingAPI_thenDeserializeResponse() {
|
fun whenMakeGETPostRequestUsingRoutingAPI_thenDeserializeResponse() {
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
package com.baeldung.kotlin
|
package com.baeldung.kotlin
|
||||||
|
|
||||||
import kotlinx.coroutines.experimental.*
|
import kotlinx.coroutines.*
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import java.util.concurrent.atomic.AtomicInteger
|
import java.util.concurrent.atomic.AtomicInteger
|
||||||
import kotlin.coroutines.experimental.buildSequence
|
|
||||||
import kotlin.system.measureTimeMillis
|
import kotlin.system.measureTimeMillis
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
import kotlin.test.assertTrue
|
import kotlin.test.assertTrue
|
||||||
|
@ -14,7 +13,7 @@ class CoroutinesTest {
|
||||||
@Test
|
@Test
|
||||||
fun givenBuildSequence_whenTakeNElements_thenShouldReturnItInALazyWay() {
|
fun givenBuildSequence_whenTakeNElements_thenShouldReturnItInALazyWay() {
|
||||||
//given
|
//given
|
||||||
val fibonacciSeq = buildSequence {
|
val fibonacciSeq = sequence {
|
||||||
var a = 0
|
var a = 0
|
||||||
var b = 1
|
var b = 1
|
||||||
|
|
||||||
|
@ -39,7 +38,7 @@ class CoroutinesTest {
|
||||||
@Test
|
@Test
|
||||||
fun givenLazySeq_whenTakeNElements_thenShouldReturnAllElements() {
|
fun givenLazySeq_whenTakeNElements_thenShouldReturnAllElements() {
|
||||||
//given
|
//given
|
||||||
val lazySeq = buildSequence {
|
val lazySeq = sequence {
|
||||||
print("START ")
|
print("START ")
|
||||||
for (i in 1..5) {
|
for (i in 1..5) {
|
||||||
yield(i)
|
yield(i)
|
||||||
|
@ -60,8 +59,8 @@ class CoroutinesTest {
|
||||||
val res = mutableListOf<String>()
|
val res = mutableListOf<String>()
|
||||||
|
|
||||||
//when
|
//when
|
||||||
runBlocking<Unit> {
|
runBlocking {
|
||||||
val promise = launch(CommonPool) { expensiveComputation(res) }
|
val promise = launch(Dispatchers.Default) { expensiveComputation(res) }
|
||||||
res.add("Hello,")
|
res.add("Hello,")
|
||||||
promise.join()
|
promise.join()
|
||||||
}
|
}
|
||||||
|
@ -85,7 +84,7 @@ class CoroutinesTest {
|
||||||
|
|
||||||
//when
|
//when
|
||||||
val jobs = List(numberOfCoroutines) {
|
val jobs = List(numberOfCoroutines) {
|
||||||
launch(CommonPool) {
|
launch(Dispatchers.Default) {
|
||||||
delay(1L)
|
delay(1L)
|
||||||
counter.incrementAndGet()
|
counter.incrementAndGet()
|
||||||
}
|
}
|
||||||
|
@ -101,7 +100,7 @@ class CoroutinesTest {
|
||||||
fun givenCancellableJob_whenRequestForCancel_thenShouldQuit() {
|
fun givenCancellableJob_whenRequestForCancel_thenShouldQuit() {
|
||||||
runBlocking<Unit> {
|
runBlocking<Unit> {
|
||||||
//given
|
//given
|
||||||
val job = launch(CommonPool) {
|
val job = launch(Dispatchers.Default) {
|
||||||
while (isActive) {
|
while (isActive) {
|
||||||
//println("is working")
|
//println("is working")
|
||||||
}
|
}
|
||||||
|
@ -135,8 +134,8 @@ class CoroutinesTest {
|
||||||
val delay = 1000L
|
val delay = 1000L
|
||||||
val time = measureTimeMillis {
|
val time = measureTimeMillis {
|
||||||
//given
|
//given
|
||||||
val one = async(CommonPool) { someExpensiveComputation(delay) }
|
val one = async(Dispatchers.Default) { someExpensiveComputation(delay) }
|
||||||
val two = async(CommonPool) { someExpensiveComputation(delay) }
|
val two = async(Dispatchers.Default) { someExpensiveComputation(delay) }
|
||||||
|
|
||||||
//when
|
//when
|
||||||
runBlocking {
|
runBlocking {
|
||||||
|
@ -156,8 +155,8 @@ class CoroutinesTest {
|
||||||
val delay = 1000L
|
val delay = 1000L
|
||||||
val time = measureTimeMillis {
|
val time = measureTimeMillis {
|
||||||
//given
|
//given
|
||||||
val one = async(CommonPool, CoroutineStart.LAZY) { someExpensiveComputation(delay) }
|
val one = async(Dispatchers.Default, CoroutineStart.LAZY) { someExpensiveComputation(delay) }
|
||||||
val two = async(CommonPool, CoroutineStart.LAZY) { someExpensiveComputation(delay) }
|
val two = async(Dispatchers.Default, CoroutineStart.LAZY) { someExpensiveComputation(delay) }
|
||||||
|
|
||||||
//when
|
//when
|
||||||
runBlocking {
|
runBlocking {
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
package com.baeldung.thread
|
||||||
|
|
||||||
|
import kotlinx.coroutines.*
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
|
class CoroutineUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenCreateCoroutineWithLaunchWithoutContext_thenRun() = runBlocking {
|
||||||
|
|
||||||
|
val job = launch {
|
||||||
|
println("${Thread.currentThread()} has run.")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenCreateCoroutineWithLaunchWithDefaultContext_thenRun() = runBlocking {
|
||||||
|
|
||||||
|
val job = launch(Dispatchers.Default) {
|
||||||
|
println("${Thread.currentThread()} has run.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenCreateCoroutineWithLaunchWithUnconfinedContext_thenRun() = runBlocking {
|
||||||
|
|
||||||
|
val job = launch(Dispatchers.Unconfined) {
|
||||||
|
println("${Thread.currentThread()} has run.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenCreateCoroutineWithLaunchWithDedicatedThread_thenRun() = runBlocking {
|
||||||
|
|
||||||
|
val job = launch(newSingleThreadContext("dedicatedThread")) {
|
||||||
|
println("${Thread.currentThread()} has run.")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenCreateAsyncCoroutine_thenRun() = runBlocking {
|
||||||
|
|
||||||
|
val deferred = async(Dispatchers.IO) {
|
||||||
|
return@async "${Thread.currentThread()} has run."
|
||||||
|
}
|
||||||
|
|
||||||
|
val result = deferred.await()
|
||||||
|
println(result)
|
||||||
|
}
|
||||||
|
}
|
|
@ -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.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,8 +22,19 @@
|
||||||
<id>kotlin-ktor</id>
|
<id>kotlin-ktor</id>
|
||||||
<url>https://dl.bintray.com/kotlin/ktor/</url>
|
<url>https://dl.bintray.com/kotlin/ktor/</url>
|
||||||
</repository>
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>kotlin-eap</id>
|
||||||
|
<url>http://dl.bintray.com/kotlin/kotlin-eap</url>
|
||||||
|
</repository>
|
||||||
</repositories>
|
</repositories>
|
||||||
|
|
||||||
|
<pluginRepositories>
|
||||||
|
<pluginRepository>
|
||||||
|
<id>kotlin-eap</id>
|
||||||
|
<url>http://dl.bintray.com/kotlin/kotlin-eap</url>
|
||||||
|
</pluginRepository>
|
||||||
|
</pluginRepositories>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -42,6 +53,11 @@
|
||||||
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||||
<version>${kotlin.version}</version>
|
<version>${kotlin.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-stdlib</artifactId>
|
||||||
|
<version>${kotlin.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jetbrains.kotlin</groupId>
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
<artifactId>kotlin-reflect</artifactId>
|
<artifactId>kotlin-reflect</artifactId>
|
||||||
|
@ -157,6 +173,7 @@
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-failsafe-plugin</artifactId>
|
<artifactId>maven-failsafe-plugin</artifactId>
|
||||||
<version>${maven-failsafe-plugin.version}</version>
|
<version>${maven-failsafe-plugin.version}</version>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -185,8 +202,8 @@
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<kotlin.version>1.2.61</kotlin.version>
|
<kotlin.version>1.3.0-rc-146</kotlin.version>
|
||||||
<kotlinx.version>0.25.0</kotlinx.version>
|
<kotlinx.version>0.26.1-eap13</kotlinx.version>
|
||||||
<ktor.io.version>0.9.3</ktor.io.version>
|
<ktor.io.version>0.9.3</ktor.io.version>
|
||||||
<assertj.version>3.11.0</assertj.version>
|
<assertj.version>3.11.0</assertj.version>
|
||||||
<junit.platform.version>1.2.0</junit.platform.version>
|
<junit.platform.version>1.2.0</junit.platform.version>
|
||||||
|
|
Loading…
Reference in New Issue