aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes thread safe classes... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "thread safe classes..." Watch "thread safe classes..." New topic
Author

thread safe classes...

Chetan Dodiya
Ranch Hand

Joined: Jun 27, 2008
Posts: 39
what is thread safe???
when a class is said to be thread safe???


SCJP 1.5
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Thread safe means that a piece of code can only be accessed by one Thread at a time. This uses the synchronized keyword.

If a class is said to be "thread safe" it means that all the methods in that class are synchronized, or again, can only be accessed by one Thread at a time.

Search on multithreading and synchronization for more information.

EDIT: Remember that a class can not be defined as synchronized, just the methods in the class.
[ November 12, 2008: Message edited by: Brian Legg ]

SCJA
~Currently preparing for SCJP6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19069
    
  40

Thread safe means that a piece of code can only be accessed by one Thread at a time. This uses the synchronized keyword.


I am not sure if I agree with this... IMO, Thread Safe means that the class can be accessed by multiple threads, and it will still work as documented. It could be achieved by synchronization. It could be achieved by segmenting data. It could be achieved by optimistic locking. It could even be achieved by documenting that it will throw an exception condition, under certain situations.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
I'll have to concede to the man who wrote the book on threads.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thread safe classes...