This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new 2626360ce Preserve interrupt status in
BackgroundInitializer.isInitialized (#1879)
2626360ce is described below
commit 2626360ce04cf7b1ac0001a51e35d8177a39953a
Author: Amar Ghugare <[email protected]>
AuthorDate: Wed Nov 18 18:13:14 2026 +0530
Preserve interrupt status in BackgroundInitializer.isInitialized (#1879)
Co-authored-by: Gary Gregory <[email protected]>
---
.../lang3/concurrent/BackgroundInitializer.java | 5 ++-
.../concurrent/BackgroundInitializerTest.java | 40 ++++++++++++++++++++++
2 files changed, 44 insertions(+), 1 deletion(-)
diff --git
a/src/main/java/org/apache/commons/lang3/concurrent/BackgroundInitializer.java
b/src/main/java/org/apache/commons/lang3/concurrent/BackgroundInitializer.java
index 57c8ce1ca..63b9d8ab3 100644
---
a/src/main/java/org/apache/commons/lang3/concurrent/BackgroundInitializer.java
+++
b/src/main/java/org/apache/commons/lang3/concurrent/BackgroundInitializer.java
@@ -345,7 +345,10 @@ public synchronized boolean isInitialized() {
try {
future.get();
return true;
- } catch (CancellationException | ExecutionException |
InterruptedException e) {
+ } catch (final InterruptedException e) {
+ Thread.currentThread().interrupt();
+ return false;
+ } catch (CancellationException | ExecutionException e) {
return false;
}
}
diff --git
a/src/test/java/org/apache/commons/lang3/concurrent/BackgroundInitializerTest.java
b/src/test/java/org/apache/commons/lang3/concurrent/BackgroundInitializerTest.java
index 3f31684ec..f5e954843 100644
---
a/src/test/java/org/apache/commons/lang3/concurrent/BackgroundInitializerTest.java
+++
b/src/test/java/org/apache/commons/lang3/concurrent/BackgroundInitializerTest.java
@@ -25,9 +25,15 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.time.Duration;
+import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import java.util.concurrent.FutureTask;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.RunnableFuture;
+import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
@@ -344,6 +350,40 @@ void testIsInitialized() throws ConcurrentException {
assertTrue(init.isInitialized(), "Not initialized after releasing
latch");
}
+ @Test
+ void testIsInitializedInterrupted() throws ConcurrentException,
InterruptedException {
+ final ExecutorService exec = new ThreadPoolExecutor(1, 1, 0,
TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>()) {
+ @Override
+ protected <T> RunnableFuture<T> newTaskFor(final Callable<T>
callable) {
+ return new FutureTask<T>(callable) {
+ @Override
+ public T get() throws InterruptedException,
ExecutionException {
+ // Some futures check interruption even when their
result is already available.
+ if (Thread.interrupted()) {
+ throw new InterruptedException();
+ }
+ return super.get();
+ }
+ };
+ }
+ };
+ try {
+ final AbstractBackgroundInitializerTestImpl init =
getBackgroundInitializerTestImpl(exec);
+ init.start();
+ init.get();
+ assertTrue(init.isInitialized());
+ Thread.currentThread().interrupt();
+ assertFalse(init.isInitialized());
+ assertTrue(Thread.currentThread().isInterrupted(), "Interrupt
status was cleared");
+ Thread.interrupted();
+ assertTrue(init.isInitialized());
+ } finally {
+ Thread.interrupted();
+ exec.shutdown();
+ exec.awaitTermination(1, TimeUnit.SECONDS);
+ }
+ }
+
/**
* Tests isStarted() after the background task has finished.
*/