• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Thread Safety

Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do the terms Thread Safety in java mean?We say awt components are thread safe but swing components arenot.What does it mean?
Posts: 3837
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally, a class is Thread Safe when its state remains valid even when its methods are called simultaneously from different threads. Class has to be specifically designed to be thread safe, usually by employing some synchronization mechanism internally, or by making it immutable. Immutable classes do not change their state at all and cannot therefore be brought into invalid state after construction, regardless of how many threads access them. This is one of the reasons why immutable classes are wonderful.

You can obtain many interesting links by searching for Java Thread Safety. Why Swing was not designed thread-safe is explained here.
Posts: 5167
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rachit Kumar Saxena wrote:We say awt components are thread safe

We do? Can you quote a reference for that? I'd be interested to read about it in more detail.
Ranch Hand
Posts: 443
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simple version ..

Swing methods are intended to be invoked only from the one and only EDT thread, they have no locking as this was considered slow among other things and multithreaded GUI's hard to design correctly. If you call a swing method from off the EDT thread all bets are off, though it will probably (appear to) work unless they have a guard (don't do it though).

If you say a class is thread safe you can call it from any thread without worrying about synchronization and the Java memory model (gross horrible simplification), in Swing you would use the SwingUtilities to execute your task on the EDT rather than call any method from your thread which your statements claims is ok for AWT (I'd have to check).

AWT is older than swing hence the difference.
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
the value of filler advertising in 2020
    Bookmark Topic Watch Topic
  • New Topic