Results 1 to 5 of 5
  1. #1
    Anveshan is offline Member
    Join Date
    May 2011
    Posts
    22
    Rep Power
    0

    Angry Doubt in threads

    Hi,
    I am trying to understand a program and unable to do so..Can you help me ?

    Here is the program:

    class PingPong2 {
    synchronized void hit(long n)
    {
    for(int i = 1; i < 3; i++)
    System.out.print(n + "-" + i + " ");
    }
    }


    public class Tester implements Runnable {

    static PingPong2 pp2 = new PingPong2();
    public static void main(String[] args)
    {
    new Thread(new Tester()).start();
    new Thread(new Tester()).start();
    }
    public void run()
    {
    pp2.hit(Thread.currentThread().getId());
    }
    }

    I run this program in ways:
    First Way: I run as it is and I get outputs as follows
    8-1 8-2 9-1 9-2
    9-1 9-2 8-1 8-2
    This I understood since the method hit() in PingPong2 class is synchronized and hence one thread completes it execution before it gives way to others

    Second way: I remove the word static from the line

    static PingPong2 pp2 = new PingPong2();
    i.e I just
    PingPong2 pp2 = new PingPong2();

    and execute it and I get output like this
    8-1 9-1 8-2 9-2

    This output shows that(I feel) synchronization is disturbed since different two thread ids are interlaced in the output..
    Why is that I get such output when i erase "static" from the initialization statement. How is it related ?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Because the synchronized keyword in a method declaration synchronizes on the instance of the class it belongs to, and when you declare that instance to be static then both "Tester" instances have the same PingPong2 instance and when it is not then they have different PingPong2 instances.

  3. #3
    Anveshan is offline Member
    Join Date
    May 2011
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Because the synchronized keyword in a method declaration synchronizes on the instance of the class it belongs to, and when you declare that instance to be static then both "Tester" instances have the same PingPong2 instance and when it is not then they have different PingPong2 instances.
    okay..I got it..missed that point..thank you so much..
    If i understand you correctly then this piece of code should again give synchronized output (one of these 8-1 8-2 9-1 9-2 ,9-1 9-2 8-1 8-2 )

    Tester test = new Tester();
    Thread thread1 = new Thread(test );
    Thread thread2 = new Thread(test );
    thread1.start();
    thread2.start();

    Since now same instance of runnable is invoked.

  4. #4
    Anveshan is offline Member
    Join Date
    May 2011
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Because the synchronized keyword in a method declaration synchronizes on the instance of the class it belongs to, and when you declare that instance to be static then both "Tester" instances have the same PingPong2 instance and when it is not then they have different PingPong2 instances.
    okay..I got it..missed that point..thank you so much..
    If i understand you correctly then this piece of code should again give synchronized output (one of these 8-1 8-2 9-1 9-2 ,9-1 9-2 8-1 8-2 )

    Tester test = new Tester();
    Thread thread1 = new Thread(test );
    Thread thread2 = new Thread(test );
    thread1.start();
    thread2.start();

    Since now same instance of runnable is invoked.

  5. #5
    roja.raou Guest

    Default

    Then in order to use it, you pass the object into the constructor of a Thread, and then call the Thread's start() method.

Similar Threads

  1. Doubt about 09
    By maya700 in forum New To Java
    Replies: 2
    Last Post: 08-17-2010, 06:37 PM
  2. Doubt
    By karthikeyan_raju in forum Advanced Java
    Replies: 11
    Last Post: 11-05-2009, 04:48 PM
  3. doubt
    By Chris555 in forum Networking
    Replies: 0
    Last Post: 08-05-2009, 02:58 PM
  4. doubt
    By shaju_inspira in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 01-18-2008, 08:38 AM
  5. doubt in jms
    By veena in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 01-05-2008, 01:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •