Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads with separate lock Problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



can' the output
X X X2 X2
possible
both threads have there separate locks
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohitkumar gupta wrote:
both threads have there separate locks

under synchronization block,we acquire lock on object reference not on thread.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohitkumar gupta wrote:

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



No!
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, So, you've cleared it?
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so,i think Arjun Srivastava that the both the code below are doing the same thing



 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, In the Thread's run() method,



So, in your case, both will give same output!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic