Updating swing components from an arbitrary thread
Threads that work in the background to support the runtime environment are called .
For example, the clock handler thread, the idle thread, the screen updater thread and the garbage collector thread are all daemon threads.
Unfortunately, trying to escape learning by being overly conservative also imparts a big speed penalty. Code will work most of the time then mysteriously fail after days of working fine.
Code may work fine then fail as soon as you feed it to an optimising compiler, (likely missing does, however, it might be a useful framework to use to dive in to the detail.
In one word, Since Swing is not thread-safe, you can not update Swing components from any random thread, they are always updated using Event Dispatcher thread.
This is in fact one of the most popular Java Swing Interview Question , with lot's of interesting follow-up e.g. We will see answer of this question in next section.
Also monitor some known well-behaved apps so you get an idea the sort of behaviour you should find.
You really do need a deep understanding of this, something I do not have yet.Treat it with just as much caution as you would unprotected volatile variables in a multi-threaded C or assembly language program.Reserve it for these few situations: The rest of the time, use synchronized blocks and mutex-protected critical regions since they are easier to think about and get right.Someday I will wade through the specs to find out how these keywords are supposed to work.I presume when you enter a synchronized block the compiler freshly loads all values it needs and when you exit it makes sure all are stored so that other CPUs ( data is very difficult to get solidly right.