Problem in using 2 threads on different instances of same class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Problem in using 2 threads on different instances of same class

  1. #1
    Dhiraj Nilange Guest

    Problem in using 2 threads on different instances of same class


    Hi there
    I have a very basic problem in Java's multithreading. I wanted to see
    the effect of not using synchronization in multithreading. I will at
    first try to ask my doubt in sentence and then using the actual code
    show what the problem really is.
    We know that declaring any class method as 'synchronized' prevents
    multiple threads to call any other synchronized method on the same
    instance at the same time.This also means that at the SAME TIME
    any other threads can call the SAME METHOD on DIFFERENT INSTANCE.
    But when I tried to do this I got the same effect which I used to
    get in case of the same instance.

    Given below is the first code. In this code two threads try to call
    the same method on the same instance at the same time. In this code
    I have not used synchronization. So it gives somewhat unexpected
    result , obviously. Take a look at it.


    //THE FIRST CODE STARTS HERE
    class box{
    void say()
    {
    System.out.print("[");
    try{
    Thread.sleep(10);
    } catch(InterruptedException e)
    {
    System.out.println("Box Interrupted");
    }
    System.out.print("Java 2 Complete Reference");
    System.out.print("]");
    System.out.println();
    }
    }

    class actor implements Runnable{
    box a;
    actor(box b)
    {
    Thread t=new Thread(this);
    a=b;
    t.start();
    }

    public void run()
    {
    a.say();
    }
    }

    class bad{
    public static void main(String args[])
    {
    box bx=new box();
    new actor(bx); // TWO THREADS USING SAME
    new actor(bx); // INSTANCE WHICH IS bx
    }
    }
    // THE FIRST CODE ENDS HERE

    So when I made a small change in this code this ran properly as expected.
    i.e. I made method 'void say()' synchronized as:-

    synchronized void say()
    {
    //...
    //...
    }

    But this was the case of THE SAME INSTANCES. When I changed my code little
    bit more ; i.e. Instead of calling two threads on the same instance I
    called them on two different instances. Here I didn't use synchronization
    ; because by definition it only restricts concurrent use of any method by
    more than one thread ON THE SAME METHOD ON THE SAME INSTANCE. SO WHEN I
    USED DIFFERENT INSTANCE IT SHOULDN'T HAVE GIVEN ANY PROBLEM AND RUN
    PEOPERLY. But this didn't happen. It again gave the same unexpected result
    which I got in the FIRST CODE. Then just out of curiosity I again made
    say() method synchronized. But that also made no difference in the output.
    This code I am giving below.

    // CODE FOR DIFFERENT INSTANCES STARTS HERE
    class box{
    void say() //SYNCHRONIZATION DELIBERATELY NOT USED
    {
    System.out.print("[");
    try{
    Thread.sleep(10);
    } catch(InterruptedException e)
    {
    System.out.println("Box Interrupted");
    }
    System.out.print("Java 2 Complete Reference");
    System.out.print("]");
    System.out.println();
    }
    }

    class actor implements Runnable{
    box a;
    actor(box b)
    {
    Thread t=new Thread(this);
    a=b;
    t.start();
    }

    public void run()
    {
    a.say();
    }
    }

    class bad{
    public static void main(String args[])
    {
    box bx=new box();
    box pp=new box();
    new actor(bx); // TWO THREADS USE DIFFERENT
    new actor(pp); // INSTANCES WHICH ARE bx AND pp
    }
    }
    // CODE FOR DIFFERENT INSTANCES ENDS HERE


    So would you please tell me why this unexpected output comes even after
    calling two threads on separate instances? And synchronization also
    doesn't work in that case(that's obvious).

    Please answer
    Thanks
    -Dhiraj


  2. #2
    Paul Clapham Guest

    Re: Problem in using 2 threads on different instances of same class

    It's hard to respond to this because you never showed us any of the output
    which you considered to be right or wrong. But "synchronized" works by
    putting a lock on an object. It has nothing much to do with methods. When
    you use "synchronized" on a block of code, which can be a method or just a
    line of code in a method, first the system waits for a lock on the object,
    then when it gets it it executes the code, then it releases the lock. Only
    one thread can hold one of these locks at any time.

    You can say "synchronized(thing)", which will use a lock on the object named
    "thing". If you just say "synchronized", that's the same as saying
    "synchronized(this)" and, as you said, that should not prevent synchronized
    code in different instances from running at the same time.

    PC2

    Dhiraj Nilange <dhiraj_nilange@yahoo.com> wrote in message
    news:3a33bc68$1@news.devx.com...
    >





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center