Skip to content

Commit 5f93927

Browse files
committed
core
coroutine scope
1 parent fa1f8d4 commit 5f93927

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ repositories {
99

1010
dependencies {
1111
implementation("org.redisson:redisson:3.19.3")
12+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
1213

1314
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
1415
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.9.1")

app/src/main/kotlin/redis_kotlin/App.kt

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package redis_kotlin
22

33
import io.reactivex.rxjava3.core.Completable
44
import io.reactivex.rxjava3.core.Single
5+
import kotlinx.coroutines.*
56
import org.redisson.Redisson
67
import org.redisson.RedissonMultiLock
78
import org.redisson.api.*
@@ -10,8 +11,6 @@ import org.redisson.config.Config
1011
import reactor.core.publisher.Mono
1112
import java.io.Serializable
1213
import java.time.Duration;
13-
import java.util.concurrent.TimeUnit
14-
1514

1615
data class Book(val pages: Int, val chapter: Int, val author: String) : Serializable
1716

@@ -90,34 +89,6 @@ class App {
9089
myAtomicLong.unlink() // clean up, happens async
9190
println("myAtomicLong after unlink/cleanup: $myAtomicLong")
9291
}
93-
94-
private fun atomicLongCoroutines(redisson: RedissonClient, newValue: Long) {
95-
printHelper("atomicLong corountines")
96-
val myAtomicLong: RAtomicLong = redisson.getAtomicLong("myAtomicLongCoRoutine")
97-
println("initial myAtomicLong: $myAtomicLong")
98-
99-
val job = GlobalScope.launch {
100-
for (i in 1..7) {
101-
myAtomicLong.incrementAndGet()
102-
println("myAtomicLong after increment and set: $myAtomicLong (loop run # $i)")
103-
}
104-
}
105-
// Waiting for the increments to finish
106-
job.join()
107-
108-
val jobs = mutableListOf<Job>()
109-
repeat(1) {
110-
jobs.add(GlobalScope.launch {
111-
myAtomicLong.get()
112-
println("myAtomicLong after get: $myAtomicLong")
113-
})
114-
}
115-
116-
// Waiting for the jobs to finish
117-
jobs.joinAll()
118-
myAtomicLong.unlink() // clean up, happens async
119-
println("myAtomicLong after unlink/cleanup: $myAtomicLong")
120-
}
12192

12293
private fun atomicLongReactive(redisson: RedissonClient, newValue: Long) {
12394
printHelper("atomicLong reactive interface")
@@ -162,6 +133,33 @@ class App {
162133
)
163134
}
164135

136+
private suspend fun atomicLongCoroutines(redisson: RedissonClient, newValue: Long) {
137+
printHelper("atomicLong corountines")
138+
val myAtomicLong: RAtomicLong = redisson.getAtomicLong("myAtomicLongCoRoutine")
139+
println("initial myAtomicLong: $myAtomicLong")
140+
141+
val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
142+
val incAndGetJob = scope.launch {
143+
for (i in 1..7) {
144+
myAtomicLong.incrementAndGet()
145+
println("myAtomicLong after increment and set: $myAtomicLong (loop run # $i)")
146+
}
147+
}
148+
// Waiting for the increments & gets to finish
149+
incAndGetJob.join()
150+
151+
val getJob = scope.launch {
152+
myAtomicLong.get()
153+
println("myAtomicLong after get: $myAtomicLong")
154+
}
155+
156+
// Waiting for the get job to finish
157+
getJob.join()
158+
159+
myAtomicLong.unlink() // clean up, happens async
160+
println("myAtomicLong after unlink/cleanup: $myAtomicLong")
161+
}
162+
165163
private fun bucket(redissonClient: RedissonClient, bucketName: String, value: String) {
166164
printHelper("bucket")
167165
val bucket = redissonClient.getBucket<String>(bucketName)
@@ -349,6 +347,9 @@ class App {
349347
atomicLongReactive(redisson.redissonClient, 3L)
350348
atomicLongRXJava3(redisson.redissonClient, 3L)
351349
Thread.sleep(1000) // wait for 1 second to complete RX operations
350+
runBlocking {
351+
atomicLongCoroutines(redisson.redissonClient, 3L)
352+
}
352353
bucket(redisson.redissonClient, "foo", "bar") // buckets
353354
`object`(redisson.redissonClient, 100, 10, "some author")
354355
topic(redisson.redissonClient, "new message")

0 commit comments

Comments
 (0)