Skip to content

Commit

Permalink
TestExecutorService: DefaultUncaughtExceptionHandler -> try / catch
Browse files Browse the repository at this point in the history
  • Loading branch information
safris committed Dec 30, 2023
1 parent 79eb779 commit b48f5e8
Showing 1 changed file with 56 additions and 18 deletions.
74 changes: 56 additions & 18 deletions src/main/java/org/libj/test/TestExecutorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.libj.test;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -36,15 +35,6 @@
public class TestExecutorService implements ExecutorService {
protected static final ConcurrentHashMap<Thread,Throwable> exception = new ConcurrentHashMap<>();

static {
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(final Thread t, final Throwable e) {
exception.put(t, e);
}
});
}

protected ExecutorService target;
private final ArrayList<Thread> threads = new ArrayList<>();

Expand All @@ -62,7 +52,13 @@ public TestExecutorService(final ExecutorService target) {
public void execute(final Runnable command) {
target.execute(() -> {
threads.add(Thread.currentThread());
command.run();
try {
command.run();
}
catch (final Throwable t) {
exception.put(Thread.currentThread(), t);
throw t;
}
});
}

Expand Down Expand Up @@ -90,23 +86,41 @@ public boolean isTerminated() {
public <T> Future<T> submit(final Callable<T> task) {
return target.submit(() -> {
threads.add(Thread.currentThread());
return task.call();
try {
return task.call();
}
catch (final Throwable t) {
exception.put(Thread.currentThread(), t);
throw t;
}
});
}

@Override
public <T> Future<T> submit(final Runnable task, final T result) {
return target.submit(() -> {
threads.add(Thread.currentThread());
task.run();
try {
task.run();
}
catch (final Throwable t) {
exception.put(Thread.currentThread(), t);
throw t;
}
}, result);
}

@Override
public Future<?> submit(final Runnable task) {
return target.submit(() -> {
threads.add(Thread.currentThread());
task.run();
try {
task.run();
}
catch (final Throwable t) {
exception.put(Thread.currentThread(), t);
throw t;
}
});
}

Expand All @@ -119,7 +133,13 @@ public <T> List<Future<T>> invokeAll(final Collection<? extends Callable<T>> tas
@Override
public T call() throws Exception {
threads.add(Thread.currentThread());
return task.call();
try {
return task.call();
}
catch (final Throwable t) {
exception.put(Thread.currentThread(), t);
throw t;
}
}
});
}
Expand All @@ -136,7 +156,13 @@ public <T> List<Future<T>> invokeAll(final Collection<? extends Callable<T>> tas
@Override
public T call() throws Exception {
threads.add(Thread.currentThread());
return task.call();
try {
return task.call();
}
catch (final Throwable t) {
exception.put(Thread.currentThread(), t);
throw t;
}
}
});
}
Expand All @@ -153,7 +179,13 @@ public <T> T invokeAny(final Collection<? extends Callable<T>> tasks) throws Int
@Override
public T call() throws Exception {
threads.add(Thread.currentThread());
return task.call();
try {
return task.call();
}
catch (final Throwable t) {
exception.put(Thread.currentThread(), t);
throw t;
}
}
});
}
Expand All @@ -170,7 +202,13 @@ public <T> T invokeAny(final Collection<? extends Callable<T>> tasks, final long
@Override
public T call() throws Exception {
threads.add(Thread.currentThread());
return task.call();
try {
return task.call();
}
catch (final Throwable t) {
exception.put(Thread.currentThread(), t);
throw t;
}
}
});
}
Expand Down

0 comments on commit b48f5e8

Please sign in to comment.