You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
val Dispatchers.LOOM: CoroutineDispatcher
get() = Executors.newVirtualThreadPerTaskExecutor().asCoroutineDispatcher()
And change your async { ... } call to use this dispatcher as
async(Dispatchers.LOOM) { ... }
Note your controlTest can have both a Thread { ... } version and a Thread.startVirtualThread { ... } version since nothing stops Kotlin from using LOOM virtual threads directly.
A lot of this code in the samples can be shared between them as Kotlin has no problem accessing Java classes, and most concepts in Kotlin are readable from Java as well. Unless you are worried about performance of basic code between the two, the total codebase could be reduced.
sample run-local with Java, Kotlin coroutines, Kotlin w/Loom async coroutines
findAllUniqueWordsWithCount - All Words with count
n/a
n/a
10000
1031
2081
1261
Prototype
2023-01-15T15:03:16.509137
revertText - Reverted Text
O(n)
O(1)
10000
324
866
594
Prototype
2023-01-15T15:03:16.509170
contentSplitIterateSubtractAndSum - Double iteration of an array of words
O(n^2)
O(1)
10000
373
1496
1120
Prototype
2023-01-15T15:03:16.509198
repetitionCount - Repetition count
O(n^2)
O(n)
10000
2238
2734
721
Prototype
2023-01-15T15:03:16.509223
createAvlTree - Create AVL Tree
O(log n)
O(n)
10000
1197
749
178
Prototype
2023-01-15T15:03:16.509249
findPrimeSecret - Secret word in Sieve of Eratosthenes
O(n * log(log n))
O(n)
10000
556
1632
519
Prototype
2023-01-15T15:03:16.509277
createSplayTree - Create Splay Tree
O(log n)
O(n)
10000
153
604
215
Prototype
2023-01-15T15:03:16.509303
quickSort - Quick sort
O(n * log n)
O(log n)
10000
1057
3616
2815
Prototype
2023-01-15T15:03:16.509334
makeTextFromWordFlow - Make text from word Flow
n/a
n/a
10000
889
519
147
Prototype
2023-01-15T15:03:16.509367
createIntersectionWordList - Intersection Text Algorithm
O(n)
O(n)
10000
83
956
482
Prototype
2023-01-15T15:03:16.509397
controlTest - N/A
n/a
n/a
10000
742
596
722
Prototype
2023-01-15T15:03:16.509424
generalTest - N/A
n/a
n/a
10000
182
59
173
Prototype
2023-01-15T15:03:16.509450
findAllUniqueWords - wait0Nanos
n/a
n/a
2
-1
12
21
Prototype
2023-01-15T15:03:16.509475
controlTest-Loom - N/A
n/a
n/a
10000
-1
-1
10
Prototype
That last controlTest-Loom is just Kotlin controlTest changed to use VirtualThread
@DelicateCoroutinesApi
suspend fun controlTestWithVirtualThreads(repeats: Int) {
log.info("----====>>>> Starting LOOM controlTest <<<<====----")
val startTimeT = LocalDateTime.now()
val aiThread = AtomicInteger(0)
withContext(Dispatchers.IO) {
(1..repeats).map {
Thread.startVirtualThread { aiThread.getAndIncrement() }
}.forEach { it.join() }
}
val endTimeT = LocalDateTime.now()
log.info("Imma be the main Thread")
log.info(aiThread.get().toString())
log.info("It took me {} ms to finish", Duration.between(startTimeT, endTimeT).toMillis()) }
The text was updated successfully, but these errors were encountered:
Hi @apatrida, thank you so much for your contribution. I didn't knew that it was possible to dispatch coroutines onto Loom Virtual Threads. It is indeed a good addition to this repository. Thank you for letting me know!
Nice article!
Have you considered dispatching Kotlin coroutines onto Loom VirtualThreads?
Example idea:
https://kt.academy/article/dispatcher-loom
Here just create a simple dispatcher...
And change your
async { ... }
call to use this dispatcher asNote your
controlTest
can have both aThread { ... }
version and aThread.startVirtualThread { ... }
version since nothing stops Kotlin from using LOOM virtual threads directly.A lot of this code in the samples can be shared between them as Kotlin has no problem accessing Java classes, and most concepts in Kotlin are readable from Java as well. Unless you are worried about performance of basic code between the two, the total codebase could be reduced.
sample run-local with Java, Kotlin coroutines, Kotlin w/Loom async coroutines
That last
controlTest-Loom
is just KotlincontrolTest
changed to useVirtualThread
The text was updated successfully, but these errors were encountered: