From 8dac5cccf324e827c2d63b62047c6e593146d73a Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Sat, 30 Dec 2023 15:57:24 +0000 Subject: [PATCH 1/3] Experiment with Loom on --- okhttp/src/main/kotlin/okhttp3/Dispatcher.kt | 10 +++++++++- .../main/kotlin/okhttp3/internal/platform/Platform.kt | 11 +++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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") + } } } From b1ef9386413df7b1f22087a317ba2d03218d82ee Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Sun, 31 Dec 2023 11:09:36 +0000 Subject: [PATCH 2/3] More testing --- build.gradle.kts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 781eea7553d9..296e90502c5a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -85,7 +85,7 @@ subprojects { configure { toolchain { - languageVersion.set(JavaLanguageVersion.of(17)) + languageVersion.set(JavaLanguageVersion.of(21)) } } @@ -202,7 +202,7 @@ subprojects { } tasks.withType { - sourceCompatibility = JavaVersion.VERSION_1_8.toString() + sourceCompatibility = JavaVersion.VERSION_21.toString() targetCompatibility = JavaVersion.VERSION_1_8.toString() } } @@ -213,7 +213,7 @@ subprojects { dokkaSourceSets.configureEach { reportUndocumented.set(false) skipDeprecated.set(true) - jdkVersion.set(8) + jdkVersion.set(21) perPackageOption { matchingRegex.set(".*\\.internal.*") suppress.set(true) From 9f1f9438d387203fe1125fd383e5efb648e9ba48 Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Sun, 31 Dec 2023 12:00:37 +0000 Subject: [PATCH 3/3] More testing --- okhttp/src/main/kotlin/okhttp3/Dispatcher.kt | 2 ++ .../main/kotlin/okhttp3/internal/SuppressSignatureCheck.kt | 6 ++---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/okhttp/src/main/kotlin/okhttp3/Dispatcher.kt b/okhttp/src/main/kotlin/okhttp3/Dispatcher.kt index 3ff6dddadb85..41bf94514fcf 100644 --- a/okhttp/src/main/kotlin/okhttp3/Dispatcher.kt +++ b/okhttp/src/main/kotlin/okhttp3/Dispatcher.kt @@ -24,6 +24,7 @@ import java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor import java.util.concurrent.SynchronousQueue import java.util.concurrent.ThreadPoolExecutor import java.util.concurrent.TimeUnit +import okhttp3.internal.SuppressSignatureCheck import okhttp3.internal.assertThreadDoesntHoldLock import okhttp3.internal.connection.RealCall import okhttp3.internal.connection.RealCall.AsyncCall @@ -91,6 +92,7 @@ class Dispatcher() { private var executorServiceOrNull: ExecutorService? = null + @SuppressSignatureCheck @get:Synchronized @get:JvmName("executorService") val executorService: ExecutorService get() { diff --git a/okhttp/src/main/kotlin/okhttp3/internal/SuppressSignatureCheck.kt b/okhttp/src/main/kotlin/okhttp3/internal/SuppressSignatureCheck.kt index 662d65c65550..42406f7d717f 100644 --- a/okhttp/src/main/kotlin/okhttp3/internal/SuppressSignatureCheck.kt +++ b/okhttp/src/main/kotlin/okhttp3/internal/SuppressSignatureCheck.kt @@ -2,11 +2,9 @@ package okhttp3.internal import java.lang.annotation.Documented import kotlin.annotation.AnnotationRetention.BINARY -import kotlin.annotation.AnnotationTarget.CLASS -import kotlin.annotation.AnnotationTarget.CONSTRUCTOR -import kotlin.annotation.AnnotationTarget.FUNCTION +import kotlin.annotation.AnnotationTarget.* @Retention(BINARY) @Documented -@Target(CONSTRUCTOR, CLASS, FUNCTION) +@Target(CONSTRUCTOR, CLASS, FUNCTION, PROPERTY) internal annotation class SuppressSignatureCheck