Skip to content

Commit 90d8330

Browse files
hanggaHangga-Aji Sayekti
andauthored
KTLN-877 - Update article "Parallel Operations on Kotlin Collections" (#978)
* update with flows * remove ExecutorService * add thread.sleep() * update rx java and remove executor * skip * Thread.sleep(1500) * cleanup * remove sleep and update executor * update call flowOn(Dispatchers.IO) before the flatMapMerge --------- Co-authored-by: Hangga-Aji Sayekti <[email protected]>
1 parent 32ae5c6 commit 90d8330

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

core-kotlin-modules/core-kotlin-collections-6/src/test/kotlin/com/baeldung/parallelOperationsCollections/ParallelOperationCollectionsUnitTest.kt

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@ package com.baeldung.parallelOperationsCollections
33
import io.reactivex.rxjava3.core.Observable
44
import io.reactivex.rxjava3.kotlin.toObservable
55
import io.reactivex.rxjava3.schedulers.Schedulers
6-
import kotlinx.coroutines.ExperimentalCoroutinesApi
7-
import kotlinx.coroutines.async
8-
import kotlinx.coroutines.awaitAll
6+
import kotlinx.coroutines.*
97
import kotlinx.coroutines.flow.*
10-
import kotlinx.coroutines.runBlocking
118
import org.assertj.core.api.Assertions.assertThat
129
import org.junit.jupiter.api.Test
1310
import org.slf4j.LoggerFactory
1411
import java.time.Duration
1512
import java.time.Instant
1613
import java.util.concurrent.Callable
1714
import java.util.concurrent.Executors
15+
import java.util.concurrent.Future
1816
import java.util.stream.Collectors
1917

2018

@@ -46,7 +44,7 @@ class ParallelOperationCollectionsUnitTest {
4644
this.isAdult = this.age >= 18
4745
logger.info(this.toString())
4846
}
49-
47+
5048
private fun Instant.printTotalTime() {
5149
val totalTime = Duration.between(this, Instant.now()).toMillis()
5250
logger.info("Total time taken: {} ms", totalTime)
@@ -59,7 +57,7 @@ class ParallelOperationCollectionsUnitTest {
5957

6058
val filteredPeople = people
6159
.map { person ->
62-
async {
60+
async(Dispatchers.IO) {
6361
person.setAdult()
6462
person
6563
}
@@ -79,14 +77,11 @@ class ParallelOperationCollectionsUnitTest {
7977
val startTime = Instant.now()
8078

8179
val filteredPeople = people.asFlow()
80+
.flowOn(Dispatchers.IO)
8281
.flatMapMerge { person ->
8382
flow {
84-
emit(
85-
async {
86-
person.setAdult()
87-
person
88-
}.await()
89-
)
83+
person.setAdult()
84+
emit(person)
9085
}
9186
}
9287
.filter { it.age > 15 }.toList()
@@ -105,9 +100,11 @@ class ParallelOperationCollectionsUnitTest {
105100
val observable = Observable.fromIterable(people)
106101
.flatMap(
107102
{
108-
Observable.just(it).subscribeOn(Schedulers.computation()).doOnNext { person ->
109-
person.setAdult()
110-
}
103+
Observable.just(it)
104+
.subscribeOn(Schedulers.computation())
105+
.doOnNext { person ->
106+
person.setAdult()
107+
}
111108
}, people.size // Uses maxConcurrency for the number of elements
112109
)
113110
.filter { it.age > 15 }
@@ -128,9 +125,11 @@ class ParallelOperationCollectionsUnitTest {
128125
val observable = people.toObservable()
129126
.flatMap(
130127
{
131-
Observable.just(it).subscribeOn(Schedulers.computation()).doOnNext { person ->
132-
person.setAdult()
133-
}
128+
Observable.just(it)
129+
.subscribeOn(Schedulers.computation())
130+
.doOnNext { person ->
131+
person.setAdult()
132+
}
134133
}, people.size // Uses maxConcurrency for the number of elements
135134
).filter { it.age > 15 }
136135
.toList()
@@ -166,21 +165,25 @@ class ParallelOperationCollectionsUnitTest {
166165
val startTime = Instant.now()
167166

168167
val executor = Executors.newFixedThreadPool(people.size)
169-
val futures = people
168+
val futures: List<Future<Person>> = people
170169
.map { person ->
171170
executor.submit(Callable {
172171
person.setAdult()
173172
person
174-
}).get()
173+
})
175174
}
175+
176+
val results = futures
177+
.map { it.get() }
176178
.filter { it.age > 15 }
177179
.sortedBy { it.age }
178180

179181
executor.shutdown()
180182

181183
startTime.printTotalTime()
182184

183-
futures.assertOver15AndSortedByAge()
185+
results.assertOver15AndSortedByAge()
184186
}
187+
185188
}
186189

0 commit comments

Comments
 (0)