Friday, April 20, 2012

synchronization Do and Do Nots

Its very likely who don't program often multithreading in their code to synchronize on objects that can later  create big trouble. Recently i did some mistakes and so i am writing here lesson learned from  it.
  • Never synchronize such object that can have NULL value ( at some instance of  program ).
  • Never synchronize on string literals . like ( String str="hello" ) here str is string literal not an object.
  • Better to call synchronization over object that never changes its state.
  • Avoid making synchronized methods unless its not required , since that can hold lock form  whole time during its execution.
still learning ....

No comments: