From 1494809af5624d7076b70087da8f81f31c6f61e7 Mon Sep 17 00:00:00 2001 From: BenWhitehead Date: Thu, 29 Aug 2024 15:54:40 -0400 Subject: [PATCH] fix: make ParallelCompositeUploadBlobWriteSessionConfig.ExecutorSupplier#cachedPool a singleton (#2691) --- .../ParallelCompositeUploadBlobWriteSessionConfig.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/ParallelCompositeUploadBlobWriteSessionConfig.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/ParallelCompositeUploadBlobWriteSessionConfig.java index 33ff739969..498947385e 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/ParallelCompositeUploadBlobWriteSessionConfig.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/ParallelCompositeUploadBlobWriteSessionConfig.java @@ -452,7 +452,7 @@ private ExecutorSupplier() {} */ @BetaApi public static ExecutorSupplier cachedPool() { - return new CachedSupplier(); + return CachedSupplier.INSTANCE; } /** @@ -529,12 +529,18 @@ private void writeObject(ObjectOutputStream out) throws IOException { private static class CachedSupplier extends ExecutorSupplier implements Serializable { private static final long serialVersionUID = 7768210719775319260L; + private static final CachedSupplier INSTANCE = new CachedSupplier(); @Override Executor get() { ThreadFactory threadFactory = newThreadFactory(); return Executors.newCachedThreadPool(threadFactory); } + + /** prevent java serialization from using a new instance */ + private Object readResolve() { + return INSTANCE; + } } private static class FixedSupplier extends ExecutorSupplier implements Serializable {