I have frequently faced a problem while debugging.
Sometimes a thread ends throwing an exception.
And the reason of that issue is the caller/starter of the thread.
The caller send incorrect parameter or called the thread without initializing something.
In order to find from where the particular thread was called, it takes a little extra effort, as the stacktrace is useless.
What if we could append the stacktrace of the caller thread into the called thread.
Consider the following example :
public class ThreadTest {
Thread t = new Thread("executeNonBlocking") {
@Override public void run() {
// What would be a good way to
// append callerStackTrace to the stack
// trace of this thread
System.out.println("inside");
new Throwable().printStackTrace();
}
};
public void executeNonBlocking() {
final StackTraceElement[] callerStackTrace = new Throwable().getStackTrace();
new Throwable().printStackTrace();
t.start();
}
public static void main(String[] args) {
new ThreadTest().executeNonBlocking();
}
}
Output
java.lang.Throwable
at ThreadTest.executeNonBlocking(ThreadTest.java:27)
at ThreadTest.main(ThreadTest.java:41)
inside
java.lang.Throwable
at ThreadTest$1.run(ThreadTest.java:34)
Desired output
java.lang.Throwable
at ThreadTest.executeNonBlocking(ThreadTest.java:27)
at ThreadTest.main(ThreadTest.java:41)
inside
java.lang.Throwable
at ThreadTest.executeNonBlocking(ThreadTest.java:27)
at ThreadTest.main(ThreadTest.java:41)
at ThreadTest$1.run(ThreadTest.java:34)
Edit : Here is the solution obtained after discussions with @peter-lawrey
public class StackTraceInheritingThread {
private final Runnable r;
private volatile Thread th = null;
private String title;
private boolean daemon;
private InheritedStackTrace ist ;
private static final ThreadLocal<InheritedStackTrace> tl = new ThreadLocal<InheritedStackTrace>();
public StackTraceInheritingThread(Runnable r) {
this.r = r;
}
private final class StackTraceInheritingUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override public void uncaughtException(Thread t, Throwable e) {
if(ist!=null){
e.addSuppressed(ist);
}
e.printStackTrace(System.err);
}
}
public StackTraceInheritingThread setName(String nm){
this.title = nm;
return this;
}
public StackTraceInheritingThread setDaemon(boolean daemon) {
this.daemon = daemon;
return this;
}
public void start(){
if(th!=null){
throw new IllegalStateException("Already started");
}
th = new Thread(new Runnable() {
@Override public void run() {
tl.set(ist);
r.run();
}
},title);
th.setUncaughtExceptionHandler(new StackTraceInheritingUncaughtExceptionHandler());
if(daemon)th.setDaemon(true);
ist = new InheritedStackTrace();
th.start();
}
public static Throwable getInheritedStackTrace(){
return tl.get();
}
public static StackTraceInheritingThread make(Runnable r1){
return new StackTraceInheritingThread(r1);
}
private static final class InheritedStackTrace extends Exception {
}
public static void main(String[] args) {
StackTraceInheritingThread.make(new Runnable() {
@Override
public void run() {
System.out.println("heelo");
throw new RuntimeException();
}
}).setName("ExperimentalThread").start();
}
}
You can save the Throwable used to create a thread in a thread local variable.
public enum Throwables {
;
private static final InheritableThreadLocal<Throwable> STARTING_THREAD = new InheritableThreadLocal<>();
public static void set(Throwable t) {
STARTING_THREAD.set(t);
}
public static Throwable get() {
return STARTING_THREAD.get();
}
public static void printStartingThrowable() {
Throwable throwable = get();
if (throwable == null) return;
throwable.printStackTrace();
}
public static Thread start(Runnable run, String name, boolean daemon) {
Throwable tmp = new Throwable("Started here");
Thread t = new Thread(new Runnable() {
@Override
public void run() {
set(tmp);
run.run();
}
}, name);
t.setDaemon(daemon);
t.start();
return t;
}
public static void main(String... ignored) {
try {
method();
} catch (Throwable t) {
System.err.println("\nThrown in " + Thread.currentThread());
t.printStackTrace();
printStartingThrowable();
}
start(new Runnable() {
@Override
public void run() {
try {
method();
} catch (Throwable t) {
System.err.println("\nThrown in " + Thread.currentThread());
t.printStackTrace();
printStartingThrowable();
}
}
}, "Test thread", false);
}
private static void method() {
throw new UnsupportedOperationException();
}
}
prints
Thrown in Thread[main,5,main]
java.lang.UnsupportedOperationException
at Throwables.method(Throwables.java:59)
at Throwables.main(Throwables.java:36)
Thrown in Thread[Test thread,5,main]
java.lang.UnsupportedOperationException
at Throwables.method(Throwables.java:59)
at Throwables.access$000(Throwables.java:1)
at Throwables$2.run(Throwables.java:47)
at Throwables$1.run(Throwables.java:26)
at java.lang.Thread.run(Thread.java:744)
java.lang.Throwable: Started here
at Throwables.start(Throwables.java:21)
at Throwables.main(Throwables.java:43)