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();