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