Forums Register Login

Thread Safety

+Pie Number of slices to send: Send
What do the terms Thread Safety in java mean?We say awt components are thread safe but swing components arenot.What does it mean?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
Good heavens! What have you done! Here, try to fix it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 873 times.
Similar Threads
RMI server
Synchronizing a block on an object.
multiple threads for database
Concurrency for MDB's
SCBCD still relevant and worthwhile?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 22:41:42.