It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronised and notifyAll 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 "Synchronised and notifyAll" Watch "Synchronised and notifyAll" New topic

Synchronised and notifyAll

Eleanor Leong

Joined: Mar 24, 2006
Posts: 21
The following code is inside a method. What does TThread94.class means
in the code. Is it an Object or a Class?
I think it is trying to synchronized against the TThread94 Class and
using it to notifyAll()?
It is a compile error if I just used TThread.notifyAll(), complaining
non-static method notifyAll() cannot be referenced from a static context.

Chandramouli Ram
Ranch Hand

Joined: Mar 07, 2005
Posts: 65
Hi Eleanor,

You guessed it right. I believe that the class TThread94 has some static method that's synchronized, and there are competing threads to access it. So to notify those threads, the Class "object" of TThread94 must be used, as notifiyAll() is an instance (and not static) method of class Object. That's the reason you got a compilation error when you try to invoke TThread94.notifyAll().
TThread94.class returns the class object that is created when the class TThread94 is loaded.

As Java has both class Class and class Object and because class and object are also the most frequently used terms in the language, it's kind of difficult to explain a scenario such as this with unambiguous language

Hope this helps!

Thanks & Regards,<br />Chandramouli Ram
Eleanor Leong

Joined: Mar 24, 2006
Posts: 21
Thanks very much. I understand a lot better now.
I agree. Here's the link:
subject: Synchronised and notifyAll
It's not a secret anymore!