@@ -2,13 +2,11 @@ package dev.suresh.lang
22
33import dev.suresh.*
44import io.github.oshai.kotlinlogging.KLogger
5- import java.util.concurrent.StructuredTaskScope
5+ import java.lang.classfile.ClassFile
66import java.util.stream.Gatherers
7+ import kotlin.jvm.optionals.getOrNull
78import kotlin.metadata.jvm.KotlinClassMetadata
8- import kotlin.time.Duration.Companion.seconds
9- import kotlinx.datetime.Clock
109import kotlinx.datetime.LocalDateTime
11- import kotlinx.datetime.toJavaInstant
1210import langFeatures
1311import stdlibFeatures
1412
@@ -33,41 +31,41 @@ object VThread {
3331
3432 private fun KLogger.structuredConcurrency () {
3533 info { " Structured concurrency..." }
36- val taskList =
37- StructuredTaskScope <String >().use { sts ->
38- val start = Clock .System .now()
39- val tasks =
40- (1 .. 100 ).map {
41- sts.fork {
42- when (it) {
43- in 1 .. 40 -> " Task $it "
44- in 41 .. 60 -> kotlin.error(" Error in task $it " )
45- else -> {
46- while (! Thread .currentThread().isInterrupted) {
47- debug { " Task $it ..." }
48- Thread .sleep(100 )
49- }
50- " Task $it "
51- }
52- }
53- }
54- }
55- runCatching { sts.joinUntil(start.plus(1 .seconds).toJavaInstant()) }
56- tasks
57- }
58-
59- info { " Total Tasks: ${taskList.size} " }
60- val states = taskList.groupBy { it.state() }
61- states.forEach { (t, u) -> info { " $t --> ${u.size} " } }
62- check(states[StructuredTaskScope .Subtask .State .SUCCESS ]?.size == 40 )
63- check(states[StructuredTaskScope .Subtask .State .FAILED ]?.size == 20 )
64- check(states[StructuredTaskScope .Subtask .State .UNAVAILABLE ]?.size == 40 )
65-
66- StructuredTaskScope .ShutdownOnFailure ().use {
67- val task = it.fork { " Virtual thread on ${Lang (" Kotlin" )} ${platform.name} !" }
68- it.join().throwIfFailed()
69- info { task.get() }
70- }
34+ // val taskList =
35+ // StructuredTaskScope<String>().use { sts ->
36+ // val start = Clock.System.now()
37+ // val tasks =
38+ // (1..100).map {
39+ // sts.fork {
40+ // when (it) {
41+ // in 1..40 -> "Task $it"
42+ // in 41..60 -> kotlin.error("Error in task $it")
43+ // else -> {
44+ // while (!Thread.currentThread().isInterrupted) {
45+ // debug { "Task $it ..." }
46+ // Thread.sleep(100)
47+ // }
48+ // "Task $it"
49+ // }
50+ // }
51+ // }
52+ // }
53+ // runCatching { sts.joinUntil(start.plus(1.seconds).toJavaInstant()) }
54+ // tasks
55+ // }
56+ //
57+ // info { "Total Tasks: ${taskList.size}" }
58+ // val states = taskList.groupBy { it.state() }
59+ // states.forEach { (t, u) -> info { "$t --> ${u.size}" } }
60+ // check(states[StructuredTaskScope.Subtask.State.SUCCESS]?.size == 40)
61+ // check(states[StructuredTaskScope.Subtask.State.FAILED]?.size == 20)
62+ // check(states[StructuredTaskScope.Subtask.State.UNAVAILABLE]?.size == 40)
63+ //
64+ // StructuredTaskScope.ShutdownOnFailure().use {
65+ // val task = it.fork { "Virtual thread on ${Lang("Kotlin")} ${platform.name}!" }
66+ // it.join().throwIfFailed()
67+ // info { task.get() }
68+ // }
7169 }
7270
7371 private fun gatherers (): List <String > {
@@ -90,11 +88,11 @@ object VThread {
9088}
9189
9290fun classFileApi () {
93- // val codeModel =
94- // Classfile .of()
95- // .parse(Class.forName("AppKt").toBytes())
96- // .methods()
97- // .filter { it.methodName().equalsString("main") }
98- // .firstNotNullOfOrNull { it.code().getOrNull() }
99- // codeModel?.elementList()?.forEach { log.info {it.toString()} }
91+ val codeModel =
92+ ClassFile .of()
93+ .parse(Class .forName(" dev.suresh. AppKt" ).toBytes())
94+ .methods()
95+ .filter { it.methodName().equalsString(" main" ) }
96+ .firstNotNullOfOrNull { it.code().getOrNull() }
97+ codeModel?.elementList()?.forEach { log.info { it.toString() } }
10098}
0 commit comments