13 January 2021

Synchronization

  • In java a class or an object can be synchronized
  • Can’t be used with the variables
  • Synchronization block or method will be very slow
  • Synchronization can be done using keyword synchronized or volatile
  • Concurrent access of shared object provides two kinds of errors which is thread interference and memory consistency error
  • To avoid corruption state or unexpected behavior of the objects which has been shared across multi threads synchronization is needed
  • Synchronization is needed only if the shared object is mutable.If the thread is only for reading synchronization is not required
  • Synchronized code will be executed by only one thread
  • Synchronized block is synchronized on some objects
  • All synchronized block synchronized on some objects can have only one thread executing inside them at a time.All other threads trying to enter the synchronized block are blocked until the thread inside the synchronized block exist the block
  • Synchronization implemented with the concepts called monitor. Only one thread can own a monitor at a time.If a thread acquires a lock it meant to say that it entered the monitor. All other threads attempting to enter the locked monitor will be suspended until the first thread exist the monitor
  • Synchronization with variable, constructor ,static initializer and instance initializer will throw compile time error
  • Constructors, static initializer and instance initializer can’t be synchronized but can have an synchronized block
  • Multithreading introduces asynchronous behavior to the programs. If a thread is writing some data another thread may be reading the same data at that time. This may bring inconsistency.
  • When two or more threads need access to a shared resource there should be some way that the resource will be used only by one resource at a time. The process to achieve this is called synchronization.
  • To implement the synchronous behavior java has synchronous method. Once a thread is inside a synchronized method, no other thread can call any other synchronized method on the same object. All the other threads then wait until the first thread come out of the synchronized block.
  • When we want to synchronize access to objects of a class which was not designed for the multithreaded access and the code of the method which needs to be accessed synchronously is not available with us, in this case we cannot add the synchronized to the appropriate methods. In java we have the solution for this, put the calls to the methods (which needs to be synchronized) defined by this class inside a synchronized block in following manner.
  • Synchronized block takes one argument and is called mutex
  • If the block is defined inside static definition blocks like static methods or static initializer then the mutex must be classname.class
  • If the block is defined inside non-static definition blocks like non-static methods, instance initializer or constructor then the mutex must be the instance of  the class

No comments:

Post a Comment

Most views on this month