diff --git a/input-stream/src/main/java/software/amazon/s3/analyticsaccelerator/S3SeekableInputStreamFactory.java b/input-stream/src/main/java/software/amazon/s3/analyticsaccelerator/S3SeekableInputStreamFactory.java index d498085b..ceedc064 100644 --- a/input-stream/src/main/java/software/amazon/s3/analyticsaccelerator/S3SeekableInputStreamFactory.java +++ b/input-stream/src/main/java/software/amazon/s3/analyticsaccelerator/S3SeekableInputStreamFactory.java @@ -36,6 +36,7 @@ import software.amazon.s3.analyticsaccelerator.io.physical.impl.PhysicalIOImpl; import software.amazon.s3.analyticsaccelerator.request.ObjectClient; import software.amazon.s3.analyticsaccelerator.request.ObjectMetadata; +import software.amazon.s3.analyticsaccelerator.util.NamedThreadFactory; import software.amazon.s3.analyticsaccelerator.util.ObjectFormatSelector; import software.amazon.s3.analyticsaccelerator.util.OpenStreamInformation; import software.amazon.s3.analyticsaccelerator.util.S3URI; @@ -62,6 +63,7 @@ public class S3SeekableInputStreamFactory implements AutoCloseable { private final ExecutorService threadPool; private static final Logger LOG = LoggerFactory.getLogger(S3SeekableInputStreamFactory.class); + private static final String THREAD_FACTORY_NAME = "s3-analytics-accelerator-"; /** * Creates a new instance of {@link S3SeekableInputStreamFactory}. This factory should be used to @@ -87,7 +89,8 @@ public S3SeekableInputStreamFactory( // TODO: calling applications should be able to pass in a thread pool if they so wish this.threadPool = Executors.newFixedThreadPool( - configuration.getPhysicalIOConfiguration().getThreadPoolSize()); + configuration.getPhysicalIOConfiguration().getThreadPoolSize(), + new NamedThreadFactory(THREAD_FACTORY_NAME, true)); this.objectBlobStore = new BlobStore( objectClient, diff --git a/input-stream/src/main/java/software/amazon/s3/analyticsaccelerator/util/NamedThreadFactory.java b/input-stream/src/main/java/software/amazon/s3/analyticsaccelerator/util/NamedThreadFactory.java new file mode 100644 index 00000000..b632cf8f --- /dev/null +++ b/input-stream/src/main/java/software/amazon/s3/analyticsaccelerator/util/NamedThreadFactory.java @@ -0,0 +1,44 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package software.amazon.s3.analyticsaccelerator.util; + +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.atomic.AtomicInteger; + +/** ThreadFactory that creates named threads with configurable daemon status. */ +public class NamedThreadFactory implements ThreadFactory { + private final AtomicInteger threadNumber = new AtomicInteger(1); + private final String namePrefix; + private final boolean daemon; + + /** + * Constructor + * + * @param namePrefix name + * @param daemon is daemon + */ + public NamedThreadFactory(String namePrefix, boolean daemon) { + this.namePrefix = namePrefix; + this.daemon = daemon; + } + + @Override + public Thread newThread(Runnable r) { + Thread t = new Thread(r, namePrefix + threadNumber.getAndIncrement()); + t.setDaemon(daemon); + return t; + } +} diff --git a/input-stream/src/test/java/software/amazon/s3/analyticsaccelerator/util/NamedThreadFactoryTest.java b/input-stream/src/test/java/software/amazon/s3/analyticsaccelerator/util/NamedThreadFactoryTest.java new file mode 100644 index 00000000..7c2dfe04 --- /dev/null +++ b/input-stream/src/test/java/software/amazon/s3/analyticsaccelerator/util/NamedThreadFactoryTest.java @@ -0,0 +1,44 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package software.amazon.s3.analyticsaccelerator.util; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +class NamedThreadFactoryTest { + + @Test + void testThreadNaming() { + NamedThreadFactory factory = new NamedThreadFactory("test-", true); + + Thread thread1 = factory.newThread(() -> {}); + Thread thread2 = factory.newThread(() -> {}); + + assertEquals("test-1", thread1.getName()); + assertEquals("test-2", thread2.getName()); + } + + @Test + void testDaemonThread() { + NamedThreadFactory factory = new NamedThreadFactory("daemon-", true); + + Thread thread = factory.newThread(() -> {}); + + assertTrue(thread.isDaemon()); + } +}