Java threading for SwingWorker as tasks

A stream dump contains a lot of information. For example, if I suspect that an action is triggered more than once, then I just need to reset the stack trace each time the action is run, and then examine the stacks to trigger the erroneous action.

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

So how can I troubleshoot? Is there a clever trick of β€œredirecting” a thread dump to follow a genuine reason?

+4
source share
2 answers

You can extend SwingWorker to write the stack when it is created (or when executed, but then you need to create a different execution method, since it is final). Creating a reason is relatively expensive, although you may want to do this only when debugging (check the log level or some)

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) 
+2
source

I can tell you what you already know, but I suggest ThreadDump

http://www.oracle.com/technetwork/java/javase/tooldescr-136044.html#gbmpn

If you are using an IDE, this is good:

NetBeans http://netbeans.org/kb/docs/java/debug-multithreaded.html

I used Eclipse for this a lot. The Debugger view has the ability to visualize and track multiple threads, print the stack, and pause them.

0
source

All Articles