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 ]