From a2a4ea79a9a24732dc4ce78ce15668dea33970a6 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Thu, 15 Jan 2026 14:22:24 +0800 Subject: [PATCH] Optimize coroutine dispatcher in V2RayTestService --- .../java/com/v2ray/ang/service/V2RayTestService.kt | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayTestService.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayTestService.kt index 5c13514a..5d7a5169 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayTestService.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayTestService.kt @@ -7,9 +7,7 @@ import com.v2ray.ang.AppConfig import com.v2ray.ang.AppConfig.MSG_MEASURE_CONFIG import com.v2ray.ang.AppConfig.MSG_MEASURE_CONFIG_CANCEL import com.v2ray.ang.AppConfig.MSG_MEASURE_CONFIG_SUCCESS -import com.v2ray.ang.dto.EConfigType import com.v2ray.ang.extension.serializable -import com.v2ray.ang.handler.MmkvManager import com.v2ray.ang.handler.SettingsManager import com.v2ray.ang.handler.V2RayNativeManager import com.v2ray.ang.handler.V2rayConfigManager @@ -17,18 +15,18 @@ import com.v2ray.ang.util.MessageUtil import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.cancelChildren import kotlinx.coroutines.joinAll import kotlinx.coroutines.launch +import java.util.concurrent.Executors import java.util.concurrent.atomic.AtomicInteger class V2RayTestService : Service() { private val realTestJob = SupervisorJob() - private val realDispatcher = Dispatchers.IO.limitedParallelism( - Runtime.getRuntime().availableProcessors() * 3 - ) + private val cpu = Runtime.getRuntime().availableProcessors().coerceAtLeast(1) + private val realDispatcher = Executors.newFixedThreadPool((cpu * 2)).asCoroutineDispatcher() private val realTestScope = CoroutineScope( realTestJob + realDispatcher + CoroutineName("RealTest") ) @@ -60,6 +58,7 @@ class V2RayTestService : Service() { override fun onDestroy() { super.onDestroy() realTestJob.cancel() + realDispatcher.close() } /**