From 4509b295c059e76ea82eaa8180d82704a16c5d33 Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Tue, 13 Feb 2024 14:32:45 -0600 Subject: [PATCH] Clean up compilation warnings There are several compilation warnings that we can clean up, mostly regarding deprecated member access and unchecked generics usage. --- .../jboss/threads/EnhancedQueueExecutor.java | 19 ++++++++----- .../org/jboss/threads/JBossExecutors.java | 1 + .../org/jboss/threads/JBossThreadFactory.java | 1 + src/main/java/org/jboss/threads/Messages.java | 9 ++++-- .../threads/EnhancedQueueExecutorTest.java | 13 +++++---- .../EnhancedThreadQueueExecutorTestCase.java | 28 +++++++++---------- 6 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/jboss/threads/EnhancedQueueExecutor.java b/src/main/java/org/jboss/threads/EnhancedQueueExecutor.java index c29c278..400d335 100644 --- a/src/main/java/org/jboss/threads/EnhancedQueueExecutor.java +++ b/src/main/java/org/jboss/threads/EnhancedQueueExecutor.java @@ -557,6 +557,9 @@ public long getKeepAliveTime(TimeUnit keepAliveUnits) { */ public Builder setKeepAliveTime(final Duration keepAliveTime) { Assert.checkNotNullParam("keepAliveTime", keepAliveTime); + if (keepAliveTime.compareTo(Duration.ZERO) <= 0) { + throw Messages.msg.nonPositiveKeepAlive(keepAliveTime); + } this.keepAliveTime = keepAliveTime; return this; } @@ -572,10 +575,8 @@ public Builder setKeepAliveTime(final Duration keepAliveTime) { */ @Deprecated public Builder setKeepAliveTime(final long keepAliveTime, final TimeUnit keepAliveUnits) { - Assert.checkMinimumParameter("keepAliveTime", 1L, keepAliveTime); Assert.checkNotNullParam("keepAliveUnits", keepAliveUnits); - this.keepAliveTime = Duration.of(keepAliveTime, JDKSpecific.timeToTemporal(keepAliveUnits)); - return this; + return setKeepAliveTime(Duration.of(keepAliveTime, JDKSpecific.timeToTemporal(keepAliveUnits))); } /** @@ -2481,7 +2482,6 @@ final class Task implements Runnable { } @Override - @SuppressWarnings("rawtypes") public void run() { if (isShutdownInterrupt(threadStatus)) { Thread.currentThread().interrupt(); @@ -2492,7 +2492,7 @@ public void run() { final Thread currentThread = Thread.currentThread(); final ClassLoader old = JBossExecutors.getAndSetContextClassLoader(currentThread, contextClassLoader); try { - ((ContextHandler)contextHandler).runWith(delegate, context); + doRunWith(delegate, context); } catch (Throwable t) { try { exceptionHandler.uncaughtException(Thread.currentThread(), t); @@ -2511,6 +2511,11 @@ public void run() { } } + @SuppressWarnings("unchecked") + private void doRunWith(Runnable delegate, Object context) { + ((ContextHandler)contextHandler).runWith(delegate, (T) context); + } + /** * Extracts the original runnable without EQE-specific state updating. This runnable does retain the original * context classloader from the submitting thread. @@ -2627,6 +2632,7 @@ public boolean cancel(final boolean mayInterruptIfRunning) { } } + @SuppressWarnings("unchecked") public V get() throws InterruptedException, ExecutionException { int state; synchronized (this) { @@ -2649,7 +2655,6 @@ public V get() throws InterruptedException, ExecutionException { throw new ExecutionException((Throwable) result); } case ASF_ST_FINISHED: { - //noinspection unchecked return (V) result; } } @@ -2657,6 +2662,7 @@ public V get() throws InterruptedException, ExecutionException { } } + @SuppressWarnings("unchecked") public V get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { long remaining = unit.toNanos(timeout); long start = System.nanoTime(); @@ -2684,7 +2690,6 @@ public V get(final long timeout, final TimeUnit unit) throws InterruptedExceptio throw new ExecutionException((Throwable) result); } case ASF_ST_FINISHED: { - //noinspection unchecked return (V) result; } } diff --git a/src/main/java/org/jboss/threads/JBossExecutors.java b/src/main/java/org/jboss/threads/JBossExecutors.java index 4f61fc7..91752d3 100644 --- a/src/main/java/org/jboss/threads/JBossExecutors.java +++ b/src/main/java/org/jboss/threads/JBossExecutors.java @@ -17,6 +17,7 @@ /** * JBoss thread- and executor-related utility and factory methods. */ +@SuppressWarnings("deprecation") public final class JBossExecutors { private static final Logger THREAD_ERROR_LOGGER = Logger.getLogger("org.jboss.threads.errors"); diff --git a/src/main/java/org/jboss/threads/JBossThreadFactory.java b/src/main/java/org/jboss/threads/JBossThreadFactory.java index d432da0..ff14082 100644 --- a/src/main/java/org/jboss/threads/JBossThreadFactory.java +++ b/src/main/java/org/jboss/threads/JBossThreadFactory.java @@ -58,6 +58,7 @@ public JBossThreadFactory(ThreadGroup threadGroup, final Boolean daemon, final I /** * @deprecated Use {@link #JBossThreadFactory(ThreadGroup, Boolean, Integer, String, Thread.UncaughtExceptionHandler, Long)} instead. */ + @Deprecated public JBossThreadFactory(ThreadGroup threadGroup, final Boolean daemon, final Integer initialPriority, String namePattern, final Thread.UncaughtExceptionHandler uncaughtExceptionHandler, final Long stackSize, final AccessControlContext ignored) { this(threadGroup, daemon, initialPriority, namePattern, uncaughtExceptionHandler, stackSize); } diff --git a/src/main/java/org/jboss/threads/Messages.java b/src/main/java/org/jboss/threads/Messages.java index 757a7c0..7e87533 100644 --- a/src/main/java/org/jboss/threads/Messages.java +++ b/src/main/java/org/jboss/threads/Messages.java @@ -1,5 +1,7 @@ package org.jboss.threads; +import java.time.Duration; + import org.jboss.logging.BasicLogger; import org.jboss.logging.Logger; import org.jboss.logging.annotations.Cause; @@ -63,9 +65,7 @@ interface Messages extends BasicLogger { @Message(id = 103, value = "The current thread does not support interrupt handlers") IllegalStateException noInterruptHandlers(); - @Message(id = 104, value = "Executor is not shut down") - @Deprecated - IllegalStateException notShutDown(); +// @Message(id = 104, value = "Executor is not shut down") // @Message(id = 105, value = "Concurrent modification of collection detected") @@ -77,6 +77,9 @@ interface Messages extends BasicLogger { @LogMessage(level = Logger.Level.ERROR) void interruptHandlerThrew(@Cause Throwable cause, InterruptHandler interruptHandler); + @Message(id = 109, value = "Keep-alive time must be positive but was %s") + IllegalArgumentException nonPositiveKeepAlive(Duration actual); + // security @Message(id = 200, value = "%s() not allowed on container-managed executor") diff --git a/src/test/java/org/jboss/threads/EnhancedQueueExecutorTest.java b/src/test/java/org/jboss/threads/EnhancedQueueExecutorTest.java index b7eeca3..562c25d 100644 --- a/src/test/java/org/jboss/threads/EnhancedQueueExecutorTest.java +++ b/src/test/java/org/jboss/threads/EnhancedQueueExecutorTest.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.time.Duration; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -52,7 +53,7 @@ public void run() { @Disabled("https://issues.jboss.org/browse/JBTHR-67") public void testThreadReuse() throws TimeoutException, InterruptedException { EnhancedQueueExecutor executor = (new EnhancedQueueExecutor.Builder()) - .setKeepAliveTime(keepaliveTimeMillis, TimeUnit.MILLISECONDS) + .setKeepAliveTime(Duration.ofMillis(keepaliveTimeMillis)) .setCorePoolSize(coreSize) .setMaximumPoolSize(maxSize) .build(); @@ -81,7 +82,7 @@ public void testThreadReuse() throws TimeoutException, InterruptedException { @Disabled("https://issues.jboss.org/browse/JBTHR-67") public void testKeepaliveTime() throws TimeoutException, InterruptedException { EnhancedQueueExecutor executor = (new EnhancedQueueExecutor.Builder()) - .setKeepAliveTime(keepaliveTimeMillis, TimeUnit.MILLISECONDS) + .setKeepAliveTime(Duration.ofMillis(keepaliveTimeMillis)) .setCorePoolSize(coreSize) .setMaximumPoolSize(maxSize) .build(); @@ -104,7 +105,7 @@ public void testKeepaliveTime() throws TimeoutException, InterruptedException { @Disabled("https://issues.jboss.org/browse/JBTHR-67") public void testKeepaliveTime2() throws TimeoutException, InterruptedException { EnhancedQueueExecutor executor = (new EnhancedQueueExecutor.Builder()) - .setKeepAliveTime(keepaliveTimeMillis, TimeUnit.MILLISECONDS) + .setKeepAliveTime(Duration.ofMillis(keepaliveTimeMillis)) .setCorePoolSize(coreSize) .setMaximumPoolSize(coreSize) .build(); @@ -126,7 +127,7 @@ public void testKeepaliveTime2() throws TimeoutException, InterruptedException { @Disabled("https://issues.jboss.org/browse/JBTHR-67") public void testKeepaliveTime3() throws TimeoutException, InterruptedException { EnhancedQueueExecutor executor = (new EnhancedQueueExecutor.Builder()) - .setKeepAliveTime(keepaliveTimeMillis, TimeUnit.MILLISECONDS) + .setKeepAliveTime(Duration.ofMillis(keepaliveTimeMillis)) .allowCoreThreadTimeOut(true) .setCorePoolSize(coreSize) .setMaximumPoolSize(maxSize) @@ -146,7 +147,7 @@ public void testKeepaliveTime3() throws TimeoutException, InterruptedException { @Test public void testPrestartCoreThreads() { EnhancedQueueExecutor executor = (new EnhancedQueueExecutor.Builder()) - .setKeepAliveTime(keepaliveTimeMillis, TimeUnit.MILLISECONDS) + .setKeepAliveTime(Duration.ofMillis(keepaliveTimeMillis)) .setCorePoolSize(coreSize) .setMaximumPoolSize(maxSize) .build(); @@ -165,7 +166,7 @@ public void testStackDepth() throws InterruptedException { assertStackDepth(new EnhancedQueueExecutor.Builder() .setCorePoolSize(1) .setMaximumPoolSize(1) - .build(), expectedStackFrames + 1); + .build(), expectedStackFrames + 2); // Use a standard ThreadPoolExecutor as a baseline for comparison. assertStackDepth(Executors.newSingleThreadExecutor(), expectedStackFrames); } diff --git a/src/test/java/org/jboss/threads/EnhancedThreadQueueExecutorTestCase.java b/src/test/java/org/jboss/threads/EnhancedThreadQueueExecutorTestCase.java index a4b8eb3..7eb5242 100644 --- a/src/test/java/org/jboss/threads/EnhancedThreadQueueExecutorTestCase.java +++ b/src/test/java/org/jboss/threads/EnhancedThreadQueueExecutorTestCase.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import java.time.Duration; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -51,18 +52,17 @@ public void run() { public void testInvalidValuesKeepAliveZero() { Assertions.assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> new EnhancedQueueExecutor.Builder() - .setKeepAliveTime(0, TimeUnit.MILLISECONDS) + .setKeepAliveTime(Duration.ZERO) .setCorePoolSize(coreSize) .setMaximumPoolSize(maxSize) .build()); - ; } @Test public void testInvalidValuesKeepAliveNegative() { Assertions.assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> new EnhancedQueueExecutor.Builder() - .setKeepAliveTime(-3456, TimeUnit.MILLISECONDS) + .setKeepAliveTime(Duration.ofMillis(-3456)) .setCorePoolSize(coreSize) .setMaximumPoolSize(maxSize) .build()); @@ -72,7 +72,7 @@ public void testInvalidValuesKeepAliveNegative() { public void testInvalidValuesCoreSizeNegative() { Assertions.assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> new EnhancedQueueExecutor.Builder() - .setKeepAliveTime(keepaliveTimeMillis, TimeUnit.MILLISECONDS) + .setKeepAliveTime(Duration.ofMillis(keepaliveTimeMillis)) .setCorePoolSize(-5) .setMaximumPoolSize(maxSize) .build()); @@ -82,7 +82,7 @@ public void testInvalidValuesCoreSizeNegative() { public void testInvalidValuesMaxSizeNegative() { Assertions.assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> new EnhancedQueueExecutor.Builder() - .setKeepAliveTime(keepaliveTimeMillis, TimeUnit.MILLISECONDS) + .setKeepAliveTime(Duration.ofMillis(keepaliveTimeMillis)) .setCorePoolSize(coreSize) .setMaximumPoolSize(-3) .build()); @@ -92,7 +92,7 @@ public void testInvalidValuesMaxSizeNegative() { public void testCoreSizeBiggerThanMaxSize() { int expectedCorePoolSize = 5; EnhancedQueueExecutor executor = (new EnhancedQueueExecutor.Builder()) - .setKeepAliveTime(keepaliveTimeMillis, TimeUnit.MILLISECONDS) + .setKeepAliveTime(Duration.ofMillis(keepaliveTimeMillis)) .setCorePoolSize(2 * expectedCorePoolSize) .setMaximumPoolSize(expectedCorePoolSize) .build(); @@ -110,7 +110,7 @@ public void testCoreSizeBiggerThanMaxSize() { @Test public void testThreadReuse() throws TimeoutException, InterruptedException { EnhancedQueueExecutor executor = (new EnhancedQueueExecutor.Builder()) - .setKeepAliveTime(keepaliveTimeMillis, TimeUnit.MILLISECONDS) + .setKeepAliveTime(Duration.ofMillis(keepaliveTimeMillis)) .setCorePoolSize(coreSize) .setMaximumPoolSize(maxSize) .build(); @@ -154,7 +154,7 @@ public void testThreadReuse() throws TimeoutException, InterruptedException { @Disabled("This test consistently fails, see JBTHR-67") public void testThreadReuseAboveCoreSize() throws TimeoutException, InterruptedException { EnhancedQueueExecutor executor = (new EnhancedQueueExecutor.Builder()) - .setKeepAliveTime(60000, TimeUnit.MILLISECONDS) + .setKeepAliveTime(Duration.ofSeconds(60)) .setCorePoolSize(coreSize) .setMaximumPoolSize(maxSize) .build(); @@ -202,7 +202,7 @@ public void testThreadReuseAboveCoreSize() throws TimeoutException, InterruptedE @Disabled("This test consistently fails, see JBTHR-67") public void testKeepaliveTime() throws TimeoutException, InterruptedException { EnhancedQueueExecutor executor = (new EnhancedQueueExecutor.Builder()) - .setKeepAliveTime(keepaliveTimeMillis, TimeUnit.MILLISECONDS) + .setKeepAliveTime(Duration.ofMillis(keepaliveTimeMillis)) .setCorePoolSize(coreSize) .setMaximumPoolSize(maxSize) .allowCoreThreadTimeOut(false) @@ -241,7 +241,7 @@ public void testKeepaliveTime() throws TimeoutException, InterruptedException { @Test public void testKeepaliveTime2() throws TimeoutException, InterruptedException { EnhancedQueueExecutor executor = (new EnhancedQueueExecutor.Builder()) - .setKeepAliveTime(keepaliveTimeMillis, TimeUnit.MILLISECONDS) + .setKeepAliveTime(Duration.ofMillis(keepaliveTimeMillis)) .setCorePoolSize(coreSize) .setMaximumPoolSize(coreSize) .build(); @@ -267,7 +267,7 @@ public void testKeepaliveTime2() throws TimeoutException, InterruptedException { @Test public void testKeepaliveTimeWithCoreThreadTimeoutEnabled() throws TimeoutException, InterruptedException { EnhancedQueueExecutor executor = (new EnhancedQueueExecutor.Builder()) - .setKeepAliveTime(keepaliveTimeMillis, TimeUnit.MILLISECONDS) + .setKeepAliveTime(Duration.ofMillis(keepaliveTimeMillis)) .allowCoreThreadTimeOut(true) .setCorePoolSize(coreSize) .setMaximumPoolSize(maxSize) @@ -294,7 +294,7 @@ public void testKeepaliveTimeWithCoreThreadTimeoutEnabled() throws TimeoutExcept @Test public void testPrestartCoreThreads() { EnhancedQueueExecutor executor = (new EnhancedQueueExecutor.Builder()) - .setKeepAliveTime(keepaliveTimeMillis, TimeUnit.MILLISECONDS) + .setKeepAliveTime(Duration.ofMillis(keepaliveTimeMillis)) .setCorePoolSize(coreSize) .setMaximumPoolSize(maxSize) .build(); @@ -341,7 +341,7 @@ public void testEnhancedExecutorShutdownNoTasks() throws Exception { final CountDownLatch terminateLatch = new CountDownLatch(1); EnhancedQueueExecutor executor = new EnhancedQueueExecutor.Builder() .setCorePoolSize(10) - .setKeepAliveTime(1, TimeUnit.NANOSECONDS) + .setKeepAliveTime(Duration.ofNanos(1)) .setTerminationTask(new Runnable() { @Override public void run() { @@ -359,7 +359,7 @@ public void testEnhancedExecutorShutdown() throws Exception { final CountDownLatch terminateLatch = new CountDownLatch(1); EnhancedQueueExecutor executor = new EnhancedQueueExecutor.Builder() .setCorePoolSize(10) - .setKeepAliveTime(1, TimeUnit.NANOSECONDS) + .setKeepAliveTime(Duration.ofNanos(1)) .setTerminationTask(new Runnable() { @Override public void run() {