Параллельный поток Kotlin/Java в виртуальных потоках

У меня есть проект на Котлине с Java 19 (с экспериментальными функциями). У меня есть код:

      private val virtualExecutor = Executors.newVirtualThreadPerTaskExecutor()


private fun countTenantsConsumption(
        paged: Page<Tenant>,
        command: RequestCommand
    ): List<TenantConsumption> {
        val callable = Callable {
            paged.payload!!.parallelStream()
                .map { Pair(it, callForTenantData(command.tenantDataCommand(it))) }
                .map { TenantDataProcessor(it.first, it.second, factorService).process() }
                .collect(Collectors.toList())
        }

        return virtualExecutor.submit(callable).get()
    }

и я функционируюcallForTenantDataимеет регистратор внутри:

      private fun callForTenantData(readingCallCommand: ReadingCallCommand): List<PeriodDeviceReading> {
        val start = System.currentTimeMillis()
        val data = //call for data
        logger.info("Loading data on ${Thread.currentThread().threadId()}, virtual: ${Thread.currentThread().isVirtual} took: ${System.currentTimeMillis() - start}ms")
        return data
    }

и я получаю вывод:

      [ForkJoinPool.commonPool-worker-18] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 138, virtual: false took: 232ms
[ForkJoinPool.commonPool-worker-11] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 96, virtual: false took: 231ms
[ForkJoinPool.commonPool-worker-19] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 139, virtual: false took: 231ms
[ForkJoinPool.commonPool-worker-2] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 87, virtual: false took: 230ms
[ForkJoinPool.commonPool-worker-4] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 89, virtual: false took: 232ms
[ForkJoinPool.commonPool-worker-7] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 92, virtual: false took: 237ms
[ForkJoinPool.commonPool-worker-20] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 140, virtual: false took: 239ms
[ForkJoinPool.commonPool-worker-21] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 141, virtual: false took: 280ms
[ForkJoinPool.commonPool-worker-16] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 137, virtual: false took: 282ms
[ForkJoinPool.commonPool-worker-9] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 94, virtual: false took: 282ms
[ForkJoinPool.commonPool-worker-10] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 95, virtual: false took: 282ms
[ForkJoinPool.commonPool-worker-13] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 98, virtual: false took: 281ms
[ForkJoinPool.commonPool-worker-6] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 91, virtual: false took: 282ms
[ForkJoinPool.commonPool-worker-17] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 136, virtual: false took: 281ms
[ForkJoinPool.commonPool-worker-2] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 87, virtual: false took: 50ms
[] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 134, virtual: true took: 282ms
[ForkJoinPool.commonPool-worker-1] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 86, virtual: false took: 285ms
[ForkJoinPool.commonPool-worker-20] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 140, virtual: false took: 35ms
[ForkJoinPool.commonPool-worker-16] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 137, virtual: false took: 50ms
[ForkJoinPool.commonPool-worker-6] c.i.n.d.c.TenantdatasErviCeimpl     : Loading data on 91, virtual: false took: 50ms

Только первый элемент в потоке обрабатывается виртуальным потоком. Почему ? Как исправить это для обработки всего потока в виртуальных потоках?

0 ответов

Другие вопросы по тегам