12:07 AM
0

     Synchronized is the keyword applicable for methods and blocks but not for classes and variables.If a method or block declared as the synchronized then at a time only one Thread is allow to execute that method or block on the given object.

    The main advantage of synchronized keyword is we can resolve date inconsistency problems.But the main disadvantage of synchronized keyword is it increases waiting time of the Thread and effects performance of the system.

  Hence if there is no specific requirement then never recommended to use synchronized keyword.Internally synchronization concept is implemented by using lock concept.

   Every object in java has a unique lock.Whenever we are using synchronized keyword then only lock   concept will come into the picture.

   If a Thread wants to execute any synchronized method on the given object 1st it has to get the lock of that object. Once a Thread got the lock of that object then it’s allow to execute any synchronized method on that object.

  If the synchronized method execution completes then automatically Thread releases lock. While a Thread executing any synchronized method the remaining Threads are not allowed execute any synchronized method on that object simultaneously. But remaining Threads are allowed to execute any non-synchronized method simultaneously.

Example:

class Player{
            public synchronized void wish(String name){
                        for(int i=0;i<5;i++){
                                    System.out.print("player:");
                                    try
                                    {
                                                Thread.sleep(1000);   
                                    }
                                    catch (InterruptedException e){}
                                    System.out.println(name);
 }}}
class MyThread extends Thread{
           Play11er d;

           String name;
            
           MyThread(Player d,String name){

                        this.d=d;
                        this.name=name;            }
            public void run(){
                        d.wish(name); }}
class SynchronizedDemo
{
            public static void main(String[] args)

            {
                        Player d1=new Player();

                        MyThread t1=new MyThread(d1,"dhoni");

                        MyThread t2=new MyThread(d1,"yuvaraj");

                        t1.start();

                        t2.start();
            }
}
· 
   If we are not declaring wish() method as synchronized then both Threads will be executed simultaneously and we will get irregular output.

Output:

player:player:yuvaraj
player:dhoni
player:yuvaraj
player:dhoni
player:yuvaraj
player:dhoni
player:yuvaraj
player:dhoni
player:yuvaraj
dhoni

·  If we declare wish()method as synchronized then the Threads will be executed one by one  that is until completing the 1st Thread the 2nd Thread will wait in this case we will get regular output which is nothing but

Output:
player:dhoni
player:dhoni
player:dhoni
player:dhoni
player:dhoni
player:yuvaraj
player:yuvaraj
player:yuvaraj
player:yuvaraj

player:yuvaraj


0 comments:

Post a Comment