Re: Pass by reference or value?
"Jim Korman" <jkorman@alltel.net> wrote in message
news:qn7153576fs7e11nfcckrjvrokqo3idbnf@4ax.com...
On 20 May 2007 11:58:33 -0700, getsanjay.sharma@gmail.com wrote:
On May 20, 1:07 am, Eric Sosman <esos...@acm-dot-org.invalid> wrote:
// synchronized ("string")
{
Thread t = new Thread(new Runnable() {
public void run() {
synchronized ("string") {
System.out.println("in thread");
}
}
});
t.start();
System.out.println("thread started");
t.join();
System.out.println("thread finished");
}
Thanks to Eric and the others for their enlightening advice. What I
don't understand here is why removing the commented out first line
makes the program go in loop? Why is it not the case with the second
synchronized block?
The outer Thread running main is holding a lock on the object "string"
It then starts the runnable which attemps to gain a lock on the same
object "string". Remember that in this case "string" has been
"intern"ed so all "string" references are the same object!
The code isn't "looping", its deadlocked waiting for a resource that
can not be released.
It's a subtle point that the two occurences of the constant "string" refer
to the same object.