Something a little technical today, but I always like a chance to save people endless frustration:
Let’s say you write a program in Swing (the Java standard graphics library). Let’s say you have something you want done in the background, without interrupting the main program – for example, maybe you want to listen for input from a stream. Should you just create a thread like always, with a class that implements Runnable or extends Thread?
Absolutely not. And it’s a bit annoying that the official Java documentation does a very bad job explaining it – the official Java documentation tells you that this is how you create threads, by implementing Runnable or extending Thread, and nowhere does it mention that this method completely does not work with Swing. Not exactly some esoteric, unknown library – Swing, the library behind pretty much every non-web thing written in Java ever.
So, future Swing programmers, if you need to create threads, please note that it’s done differently in Swing. You need to extend the SwingWorker class. What happens otherwise? Absolutely nothing, it seems. You create what you think is a thread, you let it block while listening on a stream, and your whole application hangs. Because it turns out creating a thread is meaningless when you’re in the world of Swing. As you’ll find if you look specifically for creating threads in Swing, all you need is that SwingWorker and it’ll be fine. I hope this post can save someone the day of frustrated Googling I had for it.