• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

notify confusion..?

 
Ranch Hand
Posts: 433
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


In this program when will we the lock on Object obj release..at line A, B or C?
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per my understanding i think when synchronized block complete its execution that means line C.
 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Prasun Howlader:
As per my understanding i think when synchronized block complete its execution that means line C.




I agree with Prasun, obj will be available at line C..

Actually, in case this is from a mock exam.. the code would actually crash on notify. It needs to be called on the obj Object. (unless perhaps obj and the Thread are the same objects

Regard,
Alex
[ February 18, 2008: Message edited by: Alex Belisle Turcot ]
 
Deepak Chopra
Ranch Hand
Posts: 433
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
Can you please elaborate on your last line..?
"unless thread and Obj are same Object..."
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess he means if you call notify(obj) , then its ok and code wont crash! You wont need to specify 'obj' in notify() if it refers to the current object being executed!
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nadeem Khan:
I guess he means if you call notify(obj) , then its ok and code wont crash! You wont need to specify 'obj' in notify() if it refers to the current object being executed!


I'll try to explain this as clear as possible..

Notify will release the lock on obj and notify a single thread waiting for obj (if there are 5 threads, only one will be notified. notifyAll would notify them all).

Now the example is weird, because we're in run(), but I'll assume there could be multiple thread running concurrently..

Thread waits on an object, you call notify on that object.
This is your thread saying: "I had the lock, but I give it to you now".

notify can only be called by the a thread who actually owns the lock.

so either:


or



My other comment was, "unless obj and this are the same", meaning if obj == this.. this.notify would be OK, when synchronized on obj..

I hope I didn't make too many mistake here, please anyone re-read, this is a little dense information

Regards,
Alex
[ February 18, 2008: Message edited by: Alex Belisle Turcot ]
 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yes am sorry i meant to say obj.notify() instead of notify(obj)
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just one thing about this statement

Thread waits on an object, you call notify on that object.
This is your thread saying: "I had the lock, but I give it to you now".




I think notify says "Go back to the thread pool my friend". Relinquishing the lock will only happen after the synchronized block (or method) ends
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ali Khalfan:
Just one thing about this statement


I think notify says "Go back to the thread pool my friend". Relinquishing the lock will only happen after the synchronized block (or method) ends



Yes It was definitely unclear..

Also, I wrote


Now the example is weird, because we're in run(), but I'll assume there could be multiple thread running concurrently..



After sleeping on it, there could be a synchronize block on the same object elsewhere, which would make this run() worth synchronizing. So it's not so weird after all.
[ February 18, 2008: Message edited by: Alex Belisle Turcot ]
 
Deepak Chopra
Ranch Hand
Posts: 433
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
Thanks a lots= to you all..!!!
[ February 18, 2008: Message edited by: Sunny Jain ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic