Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

'thread-safe' and 'synchronized'  RSS feed

 
kenji kunoichi
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is threadSafe and what is synchonised ?


 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronized is a keyword which prevents access to a particular block of code from two threads; it makes it behave as if it were atomic, ie rather like a single-stage procedure. Only one thread can gain access at a time.

Thread safe is a common English description for an object which cannot suffer any problems because of access by two threads at the same time.

Go through the Java Tutorial about concurrency. The Tutorial is required reading!
 
shankar reddy
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ThreadSafe can get by using Syncronization. When the Object should be accessed only one request at a time , we can use syncronization.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are other ways than synchronisation to make an object thread-safe, eg locking, or an immutable class.
 
amitabh mehra
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
There are other ways than synchronisation to make an object thread-safe, eg locking, or an immutable class.


found a link regarding thread safety by immutable class:
Are Immutable class thread safe
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!