Skip to content

Commit aa9ad7f

Browse files
authored
fix: ignore UnsupportedOperationException for virtual threads (#2866)
The virtual threads util that tries to create a virtual thread factory on JVMs that support this would fail on Java 20, because: 1. Java 20 supports virtual threads as an experimental feature. This means that the code is present. 2. The feature is by default disabled, and throws an UnsupportedOperationException. This fix takes the above into account and returns null if a user tries to create a virtual threads factory on Java 20 with experimental features disabled.
1 parent 79c30d2 commit aa9ad7f

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/ThreadFactoryUtil.java

+10
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public static ThreadFactory tryCreateVirtualThreadFactory(String baseNameFormat)
6969
} catch (ClassNotFoundException | NoSuchMethodException ignore) {
7070
return null;
7171
} catch (InvocationTargetException | IllegalAccessException e) {
72+
// Java 20 supports virtual threads as an experimental feature. It will throw an
73+
// UnsupportedOperationException if experimental features have not been enabled.
74+
if (e.getCause() instanceof UnsupportedOperationException) {
75+
return null;
76+
}
7277
throw new RuntimeException(e);
7378
}
7479
}
@@ -91,6 +96,11 @@ public static ExecutorService tryCreateVirtualThreadPerTaskExecutor(String baseN
9196
} catch (NoSuchMethodException ignore) {
9297
return null;
9398
} catch (InvocationTargetException | IllegalAccessException e) {
99+
// Java 20 supports virtual threads as an experimental feature. It will throw an
100+
// UnsupportedOperationException if experimental features have not been enabled.
101+
if (e.getCause() instanceof UnsupportedOperationException) {
102+
return null;
103+
}
94104
throw new RuntimeException(e);
95105
}
96106
}

0 commit comments

Comments
 (0)