Skip to content
Advertisement

How do you name a SwingWorker thread? Open to code or best practices

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.

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement