views:

239

answers:

3

I'm trying to debug a horrible exception that is occurring in a SwingWorker thread. If I could name my SwingWorker threads, then I could better interpret the stack traces I am receiving.

It doesn't seem possible to name a SwingWorker thread (is it?). I'm looking for any best practices or 'tricks' to help me properly identify my worker threads, short of just peppering my code w/ log statements.

+5  A: 

While you don't have access to the ThreadFactory that SwingWorker is using, you can call Thread.currentThread().setName() in your run() or call() method of whatever class the SwingWorker is running.

Just be sure that you're setting that value when the thread is actually executing the code

Kevin
Nice answer. Simple and direct.
Jonathan Feinberg
+1  A: 

The Thread used by SwingWorker is from an Executors.defaultThreadFactory which means it's from a pool. So, naming the thread could be misleading if "MyXYZThread" gets reused for another SwingWorker in the system that fails to rename the thread.

I'd extend the SwingWorker to include a name and log that name when throwing an exception.

Note: I'd rather not make the caller responsible for handling the exception, but I don't see a way to do it without altering the contract of SwingWorker.

private static abstract class MySwingWorker<X, Y> extends SwingWorker<X, Y>
{
    private final String taskName;
    public MySwingWorker(String name)
    {
        this.taskName = name;
    }
    public String getTaskName() {
        return taskName;
    }
}

Usage:

new MySwingWorker<Object,Object>("my task") {
    protected Object doInBackground() {
        try {
            return someCall();
        }
        catch(Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, 
                "Exception in " + getTaskName(), e);
            throw e;
        }
    }
}.execute();
Devon_C_Miller
A: 

You should be able to use reflection to get the thread and call setName as well. You would need to make use of the setAccessible method to do that and some digging around to find the names of the variables you need to access.

I would not prefer this method, but it may be just as "hacky" as any other way...

TofuBeer