Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

thread safe classes...

 
Chetan Dodiya
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is thread safe???
when a class is said to be thread safe???
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Henry Wong
author
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll have to concede to the man who wrote the book on threads.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic