Skip to content

Commit 55ccc6b

Browse files
Merge pull request eugenp#5395 from freddyaott/master
[BAEL-2264] Creating a Thread in Kotlin
2 parents 1fe4967 + a828ae2 commit 55ccc6b

8 files changed

Lines changed: 164 additions & 51 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+
}

‎core-kotlin/src/test/kotlin/com/baeldung/filter/SliceTest.kt‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ internal class SliceTest {
2424
assertIterableEquals(expected, actual)
2525
}
2626

27-
@Test
28-
fun whenSlicingBeyondTheRangeOfTheArray_thenContainManyNulls() {
29-
val original = arrayOf(12, 3, 34, 4)
30-
val actual = original.slice(3..8)
31-
val expected = listOf(4, null, null, null, null, null)
32-
33-
assertIterableEquals(expected, actual)
34-
}
27+
// From the 1.3 version of Kotlin APIs, slice doesn't return array of nulls but throw IndexOutOfBoundsException
28+
// @Test
29+
// fun whenSlicingBeyondTheRangeOfTheArray_thenContainManyNulls() {
30+
// val original = arrayOf(12, 3, 34, 4)
31+
// val actual = original.slice(3..8)
32+
// val expected = listOf(4, null, null, null, null, null)
33+
//
34+
// assertIterableEquals(expected, actual)
35+
// }
3536

3637
@Test
3738
fun whenSlicingBeyondRangeOfArrayWithStep_thenOutOfBoundsException() {

‎core-kotlin/src/test/kotlin/com/baeldung/fuel/FuelHttpUnitTest.kt‎

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
package com.baeldung.fuel
22

3-
import awaitObjectResult
4-
import awaitStringResponse
53
import com.github.kittinunf.fuel.Fuel
64
import com.github.kittinunf.fuel.core.FuelManager
7-
import com.github.kittinunf.fuel.core.Request
85
import com.github.kittinunf.fuel.core.interceptors.cUrlLoggingRequestInterceptor
96
import com.github.kittinunf.fuel.gson.responseObject
107
import com.github.kittinunf.fuel.httpGet
118
import com.github.kittinunf.fuel.rx.rx_object
129
import com.google.gson.Gson
13-
import kotlinx.coroutines.experimental.runBlocking
1410
import org.junit.jupiter.api.Assertions
1511
import org.junit.jupiter.api.Test
1612
import java.io.File
@@ -226,32 +222,26 @@ internal class FuelHttpUnitTest {
226222

227223
}
228224

229-
@Test
230-
fun whenMakeGETRequestUsingCoroutines_thenResponseStatusCode200() {
231-
232-
runBlocking {
233-
val (request, response, result) = Fuel.get("http://httpbin.org/get").awaitStringResponse()
234-
235-
result.fold({ data ->
236-
Assertions.assertEquals(200, response.statusCode)
237-
238-
}, { error -> })
239-
}
240-
241-
}
242225

243-
@Test
244-
fun whenMakeGETRequestUsingCoroutines_thenDeserializeResponse() {
245-
246-
247-
runBlocking {
248-
Fuel.get("https://jsonplaceholder.typicode.com/posts?id=1").awaitObjectResult(Post.Deserializer())
249-
.fold({ data ->
250-
Assertions.assertEquals(1, data.get(0).userId)
251-
}, { error -> })
252-
}
253-
254-
}
226+
// The new 1.3 coroutine APIs, aren't implemented yet in Fuel Library
227+
// @Test
228+
// fun whenMakeGETRequestUsingCoroutines_thenResponseStatusCode200() = runBlocking {
229+
// val (request, response, result) = Fuel.get("http://httpbin.org/get").awaitStringResponse()
230+
//
231+
// result.fold({ data ->
232+
// Assertions.assertEquals(200, response.statusCode)
233+
//
234+
// }, { error -> })
235+
// }
236+
237+
// The new 1.3 coroutine APIs, aren't implemented yet in Fuel Library
238+
// @Test
239+
// fun whenMakeGETRequestUsingCoroutines_thenDeserializeResponse() = runBlocking {
240+
// Fuel.get("https://jsonplaceholder.typicode.com/posts?id=1").awaitObjectResult(Post.Deserializer())
241+
// .fold({ data ->
242+
// Assertions.assertEquals(1, data.get(0).userId)
243+
// }, { error -> })
244+
// }
255245

256246
@Test
257247
fun whenMakeGETPostRequestUsingRoutingAPI_thenDeserializeResponse() {

‎core-kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesUnitTest.kt‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package com.baeldung.kotlin
22

3-
import kotlinx.coroutines.experimental.*
3+
import kotlinx.coroutines.*
44
import org.junit.Test
55
import java.util.concurrent.atomic.AtomicInteger
6-
import kotlin.coroutines.experimental.buildSequence
76
import kotlin.system.measureTimeMillis
87
import kotlin.test.assertEquals
98
import kotlin.test.assertTrue
@@ -14,7 +13,7 @@ class CoroutinesTest {
1413
@Test
1514
fun givenBuildSequence_whenTakeNElements_thenShouldReturnItInALazyWay() {
1615
//given
17-
val fibonacciSeq = buildSequence {
16+
val fibonacciSeq = sequence {
1817
var a = 0
1918
var b = 1
2019

@@ -39,7 +38,7 @@ class CoroutinesTest {
3938
@Test
4039
fun givenLazySeq_whenTakeNElements_thenShouldReturnAllElements() {
4140
//given
42-
val lazySeq = buildSequence {
41+
val lazySeq = sequence {
4342
print("START ")
4443
for (i in 1..5) {
4544
yield(i)
@@ -60,8 +59,8 @@ class CoroutinesTest {
6059
val res = mutableListOf<String>()
6160

6261
//when
63-
runBlocking<Unit> {
64-
val promise = launch(CommonPool) { expensiveComputation(res) }
62+
runBlocking {
63+
val promise = launch(Dispatchers.Default) { expensiveComputation(res) }
6564
res.add("Hello,")
6665
promise.join()
6766
}
@@ -85,7 +84,7 @@ class CoroutinesTest {
8584

8685
//when
8786
val jobs = List(numberOfCoroutines) {
88-
launch(CommonPool) {
87+
launch(Dispatchers.Default) {
8988
delay(1L)
9089
counter.incrementAndGet()
9190
}
@@ -101,7 +100,7 @@ class CoroutinesTest {
101100
fun givenCancellableJob_whenRequestForCancel_thenShouldQuit() {
102101
runBlocking<Unit> {
103102
//given
104-
val job = launch(CommonPool) {
103+
val job = launch(Dispatchers.Default) {
105104
while (isActive) {
106105
//println("is working")
107106
}
@@ -135,8 +134,8 @@ class CoroutinesTest {
135134
val delay = 1000L
136135
val time = measureTimeMillis {
137136
//given
138-
val one = async(CommonPool) { someExpensiveComputation(delay) }
139-
val two = async(CommonPool) { someExpensiveComputation(delay) }
137+
val one = async(Dispatchers.Default) { someExpensiveComputation(delay) }
138+
val two = async(Dispatchers.Default) { someExpensiveComputation(delay) }
140139

141140
//when
142141
runBlocking {
@@ -156,8 +155,8 @@ class CoroutinesTest {
156155
val delay = 1000L
157156
val time = measureTimeMillis {
158157
//given
159-
val one = async(CommonPool, CoroutineStart.LAZY) { someExpensiveComputation(delay) }
160-
val two = async(CommonPool, CoroutineStart.LAZY) { someExpensiveComputation(delay) }
158+
val one = async(Dispatchers.Default, CoroutineStart.LAZY) { someExpensiveComputation(delay) }
159+
val two = async(Dispatchers.Default, CoroutineStart.LAZY) { someExpensiveComputation(delay) }
161160

162161
//when
163162
runBlocking {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.thread
2+
3+
import kotlinx.coroutines.*
4+
import org.junit.jupiter.api.Test
5+
6+
class CoroutineUnitTest {
7+
8+
@Test
9+
fun whenCreateCoroutineWithLaunchWithoutContext_thenRun() = runBlocking {
10+
11+
val job = launch {
12+
println("${Thread.currentThread()} has run.")
13+
}
14+
15+
}
16+
17+
@Test
18+
fun whenCreateCoroutineWithLaunchWithDefaultContext_thenRun() = runBlocking {
19+
20+
val job = launch(Dispatchers.Default) {
21+
println("${Thread.currentThread()} has run.")
22+
}
23+
}
24+
25+
@Test
26+
fun whenCreateCoroutineWithLaunchWithUnconfinedContext_thenRun() = runBlocking {
27+
28+
val job = launch(Dispatchers.Unconfined) {
29+
println("${Thread.currentThread()} has run.")
30+
}
31+
}
32+
33+
@Test
34+
fun whenCreateCoroutineWithLaunchWithDedicatedThread_thenRun() = runBlocking {
35+
36+
val job = launch(newSingleThreadContext("dedicatedThread")) {
37+
println("${Thread.currentThread()} has run.")
38+
}
39+
40+
}
41+
42+
@Test
43+
fun whenCreateAsyncCoroutine_thenRun() = runBlocking {
44+
45+
val deferred = async(Dispatchers.IO) {
46+
return@async "${Thread.currentThread()} has run."
47+
}
48+
49+
val result = deferred.await()
50+
println(result)
51+
}
52+
}
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+
}

‎parent-kotlin/pom.xml‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,18 @@
2222
<id>kotlin-ktor</id>
2323
<url>https://dl.bintray.com/kotlin/ktor/</url>
2424
</repository>
25+
<repository>
26+
<id>kotlin-eap</id>
27+
<url>http://dl.bintray.com/kotlin/kotlin-eap</url>
28+
</repository>
2529
</repositories>
30+
31+
<pluginRepositories>
32+
<pluginRepository>
33+
<id>kotlin-eap</id>
34+
<url>http://dl.bintray.com/kotlin/kotlin-eap</url>
35+
</pluginRepository>
36+
</pluginRepositories>
2637

2738
<dependencyManagement>
2839
<dependencies>
@@ -42,6 +53,11 @@
4253
<artifactId>kotlin-stdlib-jdk8</artifactId>
4354
<version>${kotlin.version}</version>
4455
</dependency>
56+
<dependency>
57+
<groupId>org.jetbrains.kotlin</groupId>
58+
<artifactId>kotlin-stdlib</artifactId>
59+
<version>${kotlin.version}</version>
60+
</dependency>
4561
<dependency>
4662
<groupId>org.jetbrains.kotlin</groupId>
4763
<artifactId>kotlin-reflect</artifactId>
@@ -157,6 +173,7 @@
157173
</executions>
158174
</plugin>
159175
<plugin>
176+
<groupId>org.apache.maven.plugins</groupId>
160177
<artifactId>maven-failsafe-plugin</artifactId>
161178
<version>${maven-failsafe-plugin.version}</version>
162179
<dependencies>
@@ -185,8 +202,8 @@
185202
</build>
186203

187204
<properties>
188-
<kotlin.version>1.2.61</kotlin.version>
189-
<kotlinx.version>0.25.0</kotlinx.version>
205+
<kotlin.version>1.3.0-rc-146</kotlin.version>
206+
<kotlinx.version>0.26.1-eap13</kotlinx.version>
190207
<ktor.io.version>0.9.3</ktor.io.version>
191208
<assertj.version>3.11.0</assertj.version>
192209
<junit.platform.version>1.2.0</junit.platform.version>

0 commit comments

Comments
 (0)