diff --git a/okhttp/src/main/kotlin/okhttp3/Dispatcher.kt b/okhttp/src/main/kotlin/okhttp3/Dispatcher.kt index 29f686dfb7ba..3ff6dddadb85 100644 --- a/okhttp/src/main/kotlin/okhttp3/Dispatcher.kt +++ b/okhttp/src/main/kotlin/okhttp3/Dispatcher.kt @@ -19,6 +19,8 @@ import java.util.ArrayDeque import java.util.Collections import java.util.Deque import java.util.concurrent.ExecutorService +import java.util.concurrent.Executors +import java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor import java.util.concurrent.SynchronousQueue import java.util.concurrent.ThreadPoolExecutor import java.util.concurrent.TimeUnit @@ -26,6 +28,7 @@ import okhttp3.internal.assertThreadDoesntHoldLock import okhttp3.internal.connection.RealCall import okhttp3.internal.connection.RealCall.AsyncCall import okhttp3.internal.okHttpName +import okhttp3.internal.platform.Platform import okhttp3.internal.threadFactory /** @@ -92,8 +95,13 @@ class Dispatcher() { @get:JvmName("executorService") val executorService: ExecutorService get() { if (executorServiceOrNull == null) { - executorServiceOrNull = ThreadPoolExecutor(0, Int.MAX_VALUE, 60, TimeUnit.SECONDS, + executorServiceOrNull = if (Platform.majorVersion < 21) { + ThreadPoolExecutor(0, Int.MAX_VALUE, 60, TimeUnit.SECONDS, SynchronousQueue(), threadFactory("$okHttpName Dispatcher", false)) + } else { + @Suppress("Since15") + newVirtualThreadPerTaskExecutor() + } } return executorServiceOrNull!! } diff --git a/okhttp/src/main/kotlin/okhttp3/internal/platform/Platform.kt b/okhttp/src/main/kotlin/okhttp3/internal/platform/Platform.kt index 0afeaa48b1e9..fec4eba0ef40 100644 --- a/okhttp/src/main/kotlin/okhttp3/internal/platform/Platform.kt +++ b/okhttp/src/main/kotlin/okhttp3/internal/platform/Platform.kt @@ -293,5 +293,16 @@ open class Platform { } return result.readByteArray() } + + val majorVersion: Int by lazy { + when (val jvmSpecVersion = getJvmSpecVersion()) { + "1.8" -> 8 + else -> jvmSpecVersion.toInt() + } + } + + fun getJvmSpecVersion(): String { + return System.getProperty("java.specification.version", "unknown") + } } }