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.
prints