Skip to content

Commit

Permalink
Merge pull request #150 from dmlloyd/warn2
Browse files Browse the repository at this point in the history
Clean up compilation warnings
  • Loading branch information
dmlloyd authored Feb 14, 2024
2 parents dcaa107 + 4509b29 commit 037e5e5
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 30 deletions.
19 changes: 12 additions & 7 deletions src/main/java/org/jboss/threads/EnhancedQueueExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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)));
}

/**
Expand Down Expand Up @@ -2481,7 +2482,6 @@ final class Task implements Runnable {
}

@Override
@SuppressWarnings("rawtypes")
public void run() {
if (isShutdownInterrupt(threadStatus)) {
Thread.currentThread().interrupt();
Expand All @@ -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);
Expand All @@ -2511,6 +2511,11 @@ public void run() {
}
}

@SuppressWarnings("unchecked")
private <T> void doRunWith(Runnable delegate, Object context) {
((ContextHandler<T>)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.
Expand Down Expand Up @@ -2627,6 +2632,7 @@ public boolean cancel(final boolean mayInterruptIfRunning) {
}
}

@SuppressWarnings("unchecked")
public V get() throws InterruptedException, ExecutionException {
int state;
synchronized (this) {
Expand All @@ -2649,14 +2655,14 @@ public V get() throws InterruptedException, ExecutionException {
throw new ExecutionException((Throwable) result);
}
case ASF_ST_FINISHED: {
//noinspection unchecked
return (V) result;
}
}
}
}
}

@SuppressWarnings("unchecked")
public V get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
long remaining = unit.toNanos(timeout);
long start = System.nanoTime();
Expand Down Expand Up @@ -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;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/jboss/threads/JBossExecutors.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/jboss/threads/JBossThreadFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/jboss/threads/Messages.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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")

Expand All @@ -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")
Expand Down
13 changes: 7 additions & 6 deletions src/test/java/org/jboss/threads/EnhancedQueueExecutorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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)
Expand All @@ -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();
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand All @@ -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)
Expand All @@ -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();
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down

0 comments on commit 037e5e5

Please sign in to comment.