-5

how to make a class as thread safe without synchronization in java and which one is best way.

rajesh
  • 3

1 Answers1

4

Make the class Immutable. Read more about immutable classes at here Examples of immutable classes

Community
  • 1
  • 1
mirmdasif
  • 5,586
  • 2
  • 21
  • 27
  • which one is the best way, either synchronization or immutability or something other?(it's my interview question, so please suggest me). – rajesh Sep 06 '14 at 04:58
  • Synchronization is needed when to thread need a shared resource that may be changed. And immutability means the resource will never change. So if your Threads share a resource that may or may not be updated or changed you need Synchronization and if the resource will never change or update after creation then it's best to make it immutable. – mirmdasif Sep 06 '14 at 09:28