Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Methods belong to Thread class???

 
Wai Yip Lau
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Wai, you are right. Every object type inherits wait() method of Object class but wait() method does not belong to Thread class.
 
Fes D Gaur
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Vipin Das
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
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
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic