Synchronization in Threads

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hostel
    New Member
    • Feb 2007
    • 17

    #1

    Synchronization in Threads

    import java.util.loggi ng.Level;
    import java.util.loggi ng.Logger;

    /**
    *
    * @author Administrator
    */
    public class sync
    {
    public static void main(String[] args)
    {
    Thread t= new Thread(new class1());
    Thread t1= new Thread(new class1());

    t.start();
    t1.start();
    }
    }
    class class1 implements Runnable
    {
    static int balance=10;
    public synchronized void run()
    {
    synchronized(th is)
    {
    try {
    get();
    Thread.sleep(50 00);
    set();
    } catch (InterruptedExc eption ex) {
    Logger.getLogge r(class1.class. getName()).log( Level.SEVERE, null, ex);
    }
    }
    }
    public synchronized void set()
    {
    ++balance;
    System.out.prin tln("balance is set Thread name=" + Thread.currentT hread().getName () + " is =" + balance);
    }
    public synchronized void get()
    {
    System.out.prin tln("balance in get Thread name=" + Thread.currentT hread().getName () + "is=" + balance);
    }
    }


    output of program:
    balance in get Thread name=Thread-0 , is=10
    balance in get Thread name=Thread-1 , is=10
    balance is set Thread name=Thread-1 , is =11
    balance is set Thread name=Thread-0 , is =12


    Expected output:
    balance in get Thread name=Thread-0 ,is=10
    balance in get Thread name=Thread-0 ,is=11
    balance is set Thread name=Thread-1 , is =11
    balance is set Thread name=Thread-1 , is =12







    public synchronized void run()
    {

    try {
    get();
    Thread.sleep(50 00);
    set();
    } catch (InterruptedExc eption ex) {
    Logger.getLogge r(class1.class. getName()).log( Level.SEVERE, null, ex);
    }

    }


    According to syncronization , Thread t should first execute and than Thread t1.

    but in my case Thread t first call get() method than goes to sleep (then it should execute set() as it is LOCKED due to synchronization ) but Thread t1 is able to access the get() method , which should not happen .
    why its happening


    Thanks in advance
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    If you implement a synchronized method it will synchronize on the object itself.
    You create two class1 objects so they both synchronize on themselves, effectively
    ignoring the lock being set on the other object.

    You should/could add a static Object lock in the class1 class and synchronize
    on that one.

    kind regards,

    Jos

    Comment

    • 2008serela
      New Member
      • Jan 2008
      • 4

      #3
      Originally posted by hostel
      import java.util.loggi ng.Level;
      import java.util.loggi ng.Logger;

      /**
      *
      * @author Administrator
      */
      public class sync
      {
      public static void main(String[] args)
      {
      Thread t= new Thread(new class1());
      Thread t1= new Thread(new class1());

      t.start();
      t1.start();
      }
      }
      class class1 implements Runnable
      {
      static int balance=10;
      public synchronized void run()
      {
      synchronized(th is)
      {
      try {
      get();
      Thread.sleep(50 00);
      set();
      } catch (InterruptedExc eption ex) {
      Logger.getLogge r(class1.class. getName()).log( Level.SEVERE, null, ex);
      }
      }
      }
      public synchronized void set()
      {
      ++balance;
      System.out.prin tln("balance is set Thread name=" + Thread.currentT hread().getName () + " is =" + balance);
      }
      public synchronized void get()
      {
      System.out.prin tln("balance in get Thread name=" + Thread.currentT hread().getName () + "is=" + balance);
      }
      }


      output of program:
      balance in get Thread name=Thread-0 , is=10
      balance in get Thread name=Thread-1 , is=10
      balance is set Thread name=Thread-1 , is =11
      balance is set Thread name=Thread-0 , is =12


      Expected output:
      balance in get Thread name=Thread-0 ,is=10
      balance in get Thread name=Thread-0 ,is=11
      balance is set Thread name=Thread-1 , is =11
      balance is set Thread name=Thread-1 , is =12







      public synchronized void run()
      {

      try {
      get();
      Thread.sleep(50 00);
      set();
      } catch (InterruptedExc eption ex) {
      Logger.getLogge r(class1.class. getName()).log( Level.SEVERE, null, ex);
      }

      }


      According to syncronization , Thread t should first execute and than Thread t1.

      but in my case Thread t first call get() method than goes to sleep (then it should execute set() as it is LOCKED due to synchronization ) but Thread t1 is able to access the get() method , which should not happen .
      why its happening


      Thanks in advance

      Pls, you will change the synchronisation methods to be private method,try this..
      not sure..If u find it pls post it.

      by
      ela

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by 2008serela
        Pls, you will change the synchronisation methods to be private method,try this..
        not sure..If u find it pls post it.

        by
        ela
        No. Read Jos' post above. He's right on the money.

        Comment

        • hostel
          New Member
          • Feb 2007
          • 17

          #5
          Originally posted by r035198x
          No. Read Jos' post above. He's right on the money.


          i got the solution what i wanted .

          in order to achieve synchronization , we need to use threads on same object

          for example
          ie sample Obj = new sample();
          Thread t1 = new Thread(Obj);
          Thread t2 = new Thread(Obj);


          but i was trying to achieve synchronization on two different object using non static method, which is not possible ( for non static method , synchronization is possible for two different thread referencing samer object).
          . In order to achieve synchronization on different object u need to declare method as STATIC .

          Comment

          Working...