Friday, June 29, 2012

The "Best Ever " example for Synchronization !!!!!!!

Hi Guys,
          
              As you know that  we can discuss half of the J2SE using the statement which is executed most number of times in this Universe, that is

                    i.e: System.out.println("");

Using this statement we can explain the concept of Synchronization in this way.

  • System is a final class which can not be extended.
  • out is a static variable in side the System class of type PrintStream.
  • println() is a method if PrintStream class.
  • We have totally 10 println() methods inside of this PrintStream class.
  • each and every println() method is synchronized.
 i.e    public void println(String x) {
        synchronized (this) {
        print(x);
        newLine();
     }
    } 

Q)      why sun micro systems designed println() method in this way?
 Ans)  Because at any point of time only one thread can be inside of this block,
                                     
Q)     Then what is the use?
Ans)   To prevent form the deadlock.If multiple thread access the same code it becomes a scrap.

In this way the println() method is a best example of Synchronization.

Thanks,

Rajesh Kumar Yuvaraj



No comments:

Post a Comment