Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/main/java/org/jboss/threads/EnhancedQueueExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.lang.management.ManagementFactory;
import java.security.AccessControlContext;
import java.security.PrivilegedAction;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
Expand Down Expand Up @@ -554,6 +555,22 @@ public Builder setKeepAliveTime(final long keepAliveTime, final TimeUnit keepAli
return this;
}

/**
* Set the thread keep-alive time using a {@link Duration}.
* <p>
* This method was added to provide API compatibility with Infinispan 15.0.19+, which uses
* the Duration-based API. It delegates to {@link #setKeepAliveTime(long, TimeUnit)}.
*
* @param keepAliveTime the thread keep-alive time (must not be {@code null})
* @return this builder
* @see EnhancedQueueExecutor#setKeepAliveTime(long, TimeUnit)
* @see EnhancedQueueExecutor#setKeepAliveTime(Duration)
*/
public Builder setKeepAliveTime(final Duration keepAliveTime) {
Assert.checkNotNullParam("keepAliveTime", keepAliveTime);
return setKeepAliveTime(keepAliveTime.toNanos(), TimeUnit.NANOSECONDS);
}

/**
* Get the thread pool growth resistance. This is the average fraction of submitted tasks that will be enqueued (instead
* of causing a new thread to start) when there are no idle threads and the pool size is equal to or greater than
Expand Down Expand Up @@ -1173,6 +1190,23 @@ public void setKeepAliveTime(final long keepAliveTime, final TimeUnit keepAliveU
timeoutNanos = max(1L, keepAliveUnits.toNanos(keepAliveTime));
}

/**
* Set the thread keep-alive time using a {@link Duration}. This is the minimum length of time that idle threads
* should remain until they exit. Unless core threads are allowed to time out, threads will only exit if the
* current thread count exceeds the core limit.
* <p>
* This method was added to provide API compatibility with Infinispan 15.0.19+, which uses
* the Duration-based API. It delegates to {@link #setKeepAliveTime(long, TimeUnit)}.
*
* @param keepAliveTime the thread keep-alive time (must not be {@code null})
* @see Builder#setKeepAliveTime(Duration) Builder.setKeepAliveTime()
* @see #setKeepAliveTime(long, TimeUnit)
*/
public void setKeepAliveTime(final Duration keepAliveTime) {
Assert.checkNotNullParam("keepAliveTime", keepAliveTime);
setKeepAliveTime(keepAliveTime.toNanos(), TimeUnit.NANOSECONDS);
}

/**
* Get the maximum queue size. If the queue is full and a task cannot be immediately accepted, rejection will result.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.jboss.threads;

import java.time.Duration;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -396,4 +397,32 @@ public void run() {
executor.shutdown();
Assert.assertTrue(terminateLatch.await(10, TimeUnit.SECONDS));
}

/**
* Test that the Duration-based setKeepAliveTime method works correctly.
* This verifies compatibility with Infinispan 15.0.19+ which uses Duration API.
*/
@Test
public void testSetKeepAliveTimeWithDuration() throws Exception {
// Test Builder.setKeepAliveTime(Duration)
EnhancedQueueExecutor executor = new EnhancedQueueExecutor.Builder()
.setCorePoolSize(coreSize)
.setMaximumPoolSize(maxSize)
.setKeepAliveTime(Duration.ofMillis(keepaliveTimeMillis))
.build();

// Verify the keepalive time was set correctly
long actualKeepAliveMillis = executor.getKeepAliveTime(TimeUnit.MILLISECONDS);
Assert.assertEquals("KeepAlive time should match the value set via Duration API",
keepaliveTimeMillis, actualKeepAliveMillis);

// Test runtime setKeepAliveTime(Duration) on the executor instance
long newKeepAliveMillis = 5000;
executor.setKeepAliveTime(Duration.ofMillis(newKeepAliveMillis));
actualKeepAliveMillis = executor.getKeepAliveTime(TimeUnit.MILLISECONDS);
Assert.assertEquals("KeepAlive time should match the value set via Duration API at runtime",
newKeepAliveMillis, actualKeepAliveMillis);

executor.shutdown();
}
}
Loading