• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Threads with separate lock Problem

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



can' the output
X X X2 X2
possible
both threads have there separate locks
 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, So, you've cleared it?
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, In the Thread's run() method,



So, in your case, both will give same output!
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic