wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads with separate lock Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads with separate lock Problem" Watch "Threads with separate lock Problem" New topic
Author

Threads with separate lock Problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634




can' the output
X X X2 X2
possible
both threads have there separate locks


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

mohitkumar gupta wrote:
both threads have there separate locks

under synchronization block,we acquire lock on object reference not on thread.


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Under synchronization block,we acquire lock on object reference not on thread.


Thread t1 has lock on ns
and Thread t2 has lock on ns2



Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Try this!

You are synchronized with a single object. So, output can be predicted. There are two possiblr outputs. Could you able to fine those?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

mohitkumar gupta wrote:

Thread t1 has lock on ns
and Thread t2 has lock on ns2



No!
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

mohitkumar gupta wrote:
Thread t1 has lock on ns
and Thread t2 has lock on ns2

you assumption according to your knowledge is wrong.
i don't know if you have read the book carefully or not.
Thread always hold the lock on the object,every object has built in lock that comes in action when the object has synchronized method code or block.
does your code compiles fine?
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634



Sorry, i had put the wrong code

Thread t1 would have lock on ns
Thread t2 would have lock on ns1

Now, IS the output:
X X X2 X2
possible now ?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

mohitkumar gupta wrote:
Thread t1 has lock on ns
and Thread t2 has lock on ns2


OK, let's clear it. When a thread enters the to execute a method or block with is synchronized on an object, then the thread acquire the lock of that particular object. In the above code, you've synchronized String Literal Pool object "s", in your run() method. So, thread acquire the lock of the String Literal Pool object.
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Arjun Srivastava:
i don't know if you have read the book carefully or not.
Thread always hold the lock on the object,every object has built in lock that comes in action when the object has synchronized method code or block.



so,Thread t1 or t2 whosoever runs first would automatically acquire lock on XSync object when,it see's synchronize(this).then,that thread would not release lock until the code in synchronized block or function is complete


Am,i right now ?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

mohitkumar gupta wrote:

Sorry, i had put the wrong code

Thread t1 would have lock on ns
Thread t2 would have lock on ns1

Now, IS the output:
X X X2 X2
possible now ?

Now, your asumption is OK, But what is the logic of synchronizing here? Now, 4 possible outputs! I think!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

mohitkumar gupta wrote:
acquire lock on XSync object when,it see's synchronize(this).then,
Am,i right now ?

This isn't an object, it's a class!
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

your code doesn't compiles on my machine,may be problem with my JDK1.6,which i have installed a fresh copy
try this code given below.

mohitkumar gupta wrote:
so,Thread t1 or t2 whosoever runs first would automatically acquire lock on XSync object when,it see's synchronize(this).then,that thread would not release lock until the code in synchronized block or function is complete
Am,i right now ?

yes,XXX2X2 can be possible,try it.
see that you have two different thread references which are invoking two different methods,no use of synchronization here.
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Abimaran :
acquire lock on XSync object when,it see's synchronize(this).then,
Am,i right now ?


i mean to say XSync's object
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

OK, So, you've cleared it?
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

so,i think Arjun Srivastava that the both the code below are doing the same thing



Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Yes, In the Thread's run() method,



So, in your case, both will give same output!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads with separate lock Problem