99521 days on xHamster
5195M profile views
29852K subscribers
20087 comments left

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.

Please or register to post comments
If spammers comment on your content, only you can see and manage such comments Delete all
Programming Java threads in the real world. executes and there's the AWT/Swing thread that handles GUI events. The publication can be any arbitrary. 
20-Sep-2018 08:37

Updating swing components from an arbitrary thread introduction

Updating swing components from an arbitrary thread

Recent posts

20-Sep-2018 13:27
21-Sep-2018 01:40
21-Sep-2018 09:17
21-Sep-2018 15:15