File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Methods belong to Thread class??? 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 "Methods belong to Thread class???" Watch "Methods belong to Thread class???" New topic

Methods belong to Thread class???

Wai Yip Lau

Joined: Jun 20, 2005
Posts: 23
Somehow I saw two questions:

From a mock exam:
Which of the following methods belong to an instance of Thread?
wait() is one of the answer, the explanation is that Thread object is an Object so it has wait() method.

From a textbook:
Which of the following methods are defined in class Thread?
wait() is considered as a wrong answer.

I want to ask about the difference of the answer. Because wait() in Thread object is inherited from Object class where it is FIRST defined, so Thread object has a wait() method but doesn't define it. Am I correct? Thank You.
Nik Raut

Joined: May 23, 2005
Posts: 22

Yes Wai, you are right. Every object type inherits wait() method of Object class but wait() method does not belong to Thread class.

To be yourself is the best thing to be.
Fes D Gaur
Ranch Hand

Joined: Apr 29, 2005
Posts: 54
The idea here is that wait(), notify, and notifyall() methods belong to java.lang.Object, while other thread related methods such sleep(), yield(), join(), and setPriority(int a) belong to java.lang.Thread. So even though all objects (even Thread class objects) inherit from the Object class and can use its methods, but the methods of class Object such as wait() are part of the Object class. Further more these methods are final and so cannot be overrridden in subclasses.

Fes D Gaur
Nicky Eng
Ranch Hand

Joined: Mar 26, 2005
Posts: 378
i have a question. Interface Runnable has a method called run(), right?

But in my college textbook got one statement "The Thread class contains a method called run()."

i wonder is it means that Thread class actually provide the implementation of run() if we implements Runnable???

in final,

java.lang.Object has 3 methods : wait(), notify(), notifyAll().
Thread has : yield(), start(), sleep(), join(), isAlive(), setPriority().
Runnable has : run().

correct me if i'm wrong. thanks.
[ June 29, 2005: Message edited by: Nicky Eng ]

From NickyEng
Diploma in Computer Studies
SCJP 1.4
Formula 1 app by Maxis (Playbook)
Vipin Das
Ranch Hand

Joined: Jul 05, 2004
Posts: 47
The Thread class itself implements Runnable interface, and provide an implementation of run(). The run method simply execute the run method of a runnable object that is passed through it through the constructor of the Thread.
[ June 29, 2005: Message edited by: Vipin Das ]
Nicky Eng
Ranch Hand

Joined: Mar 26, 2005
Posts: 378
there is a question under k&b book(exam 310-035& 310-025, page 540, question 2, about class Thread.

question is:

Which two of the following methods are defined in class Thread?
A. start()
B. wait()
C. notify()
D. run()
E. terminate()
my first answer was "answer A. start()". i know the question ask me to choose TWO. but i dont understand why run() will also be the answer.

is it because Thread implements Runnable and then run() will be defined in class Thread since class Thread implements Runnable and have to provide the implementation?

my second time answering this question on the MasterExam questions i mark run() and start().

i hope i wont get wrong on this in my saturday test coming soon.
I agree. Here's the link:
subject: Methods belong to Thread class???
It's not a secret anymore!