Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java thread trobleshooting for SwingWorker like tasks

Thread dump contains wealth of information. For example, if I suspect some action fired more than once, then all I need to do is dumping stack trace each time the action is fired, then investigate the stacks for erroneous action firing.

In certain situations developers are encouraged to abandon conceptual simplicity of sequential execution. For example, Swing offers SwingWorker helper to work around limitations of single threaded EDT. Now, if I dump stack trace, it is useless, because the action is fired by SwingWorker, and there is no information on who initiated SwingWorker task.

So, how do I troubleshoot? Is there a clever trick of "redirecting" thread dump to follow the genuine cause?

like image 800
Tegiri Nenashi Avatar asked Nov 04 '22 19:11

Tegiri Nenashi


1 Answers

You can extend SwingWorker to record the stack when it is created (or when execute but then you need to create another execute method since it is final). Creating the cause is relative expensive though so you might want to do it only when debug (check log level or some such)

import java.util.concurrent.ExecutionException;
import javax.swing.SwingWorker;

public abstract class TracedSwingWorker<T, V> extends SwingWorker<T, V> {

    private final Exception cause;

    public TracedSwingWorker() {
        super();
        this.cause = new Exception("TracedSwingWorker created at:");
    }

    @Override
    protected final T doInBackground() throws Exception {
        try {
            return doWorkInBackground();
        }
        catch(Exception e) {
            if(this.cause != null) {
                Throwable cause = e;
                while(cause.getCause() != null) {
                    cause = cause.getCause();
                }

                cause.initCause(this.cause);
            }

            throw e;
        }
    }

    protected abstract T doWorkInBackground();

    // just for testing
    public static void main(String[] args) {
        new TracedSwingWorker<Void, Void>() {
            @Override
            protected Void doWorkInBackground() {
                throw new IllegalArgumentException("Exception in TracedSwingWorker!");
            }

            @Override
            protected void done() {
                try {
                    get();
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }.execute();
    }
}

prints:

java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: Exception in SwingWorker!
        at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:222)
<snip>
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:662)
    Caused by: java.lang.Exception: SwingWorker created at:
        at TracedSwingWorker.<init>(TracedSwingWorker.java:15)
        at TracedSwingWorker$2.<init>(TracedSwingWorker.java:60)
        at TracedSwingWorker.main(TracedSwingWorker.java:60)
like image 198
Walter Laan Avatar answered Nov 12 '22 15:11

Walter Laan