If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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.

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.

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.