Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which are thread instance methods?

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.wait()
2.sleep()
3.yeild()
4.start()
5.run()
6.notify() :roll:
 
Manoj Macwan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wait() : This is Object class method
sleep() : This is static method of Thread class.
yeild() : This is static method of Thread class.
start() : This is instance method
run() : This is instance method
notify(): This is Object class method

So, I think the Answer should be start(), run(). Because only these methods are related to Thread instance.

But still, I have some doubts regarding run(), as run is called by VM.
Can run() method be considered as instance method of Thread class?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15437
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find the answer to this question very quickly by looking at the API documentation of class Thread to see which of those methods are in the class and if they are non-static.
Originally posted by Manoj Macwan:
But still, I have some doubts regarding run(), as run is called by VM.
Can run() method be considered as instance method of Thread class?

Class Thread has a public, non-static run() method so yes, run() is an instance method of class Thread.

This has nothing to do with "being called by the VM", whatever you mean by that. The run() method is not some kind of "magic" method that is called in any special way.
[ August 06, 2007: Message edited by: Jesper Young ]
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Manoj Macwan:
wait() : This is Object class method
sleep() : This is static method of Thread class.
yeild() : This is static method of Thread class.
start() : This is instance method
run() : This is instance method
notify(): This is Object class method

So, I think the Answer should be start(), run(). Because only these methods are related to Thread instance.

But still, I have some doubts regarding run(), as run is called by VM.
Can run() method be considered as instance method of Thread class?



I dont get the idea behind a method being 'thread-instance-method'. If wait and notify are not Thread's method, then even the run() methnod is not native to Thread class it is derived/inherited from Runnable interface. :roll:
[ August 06, 2007: Message edited by: Akhilesh Trivedi ]
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I dont get the idea behind a method being 'thread-instance-method'.


Lets first define what is a instance method: If an instance is required to invoke an object then its called instance method , unlike static methods that do not need an instance of the class and can be invoked with the calss name. With this definition all the methods except the ones that are marked static are per Thread object that you create, Hence all those methods are Thread instance methods.
I think your question is trying to ask which of the following methods are defined/ovveridden in the Thread class and the answer to that is
start() and run().


If wait and notify are not Thread's method, : because they are not ovverriden in the Thread class, their definitions are in the Object class.

then even the run() methnod is not native to Thread class it is derived/inherited from Runnable interface. run() is only declared but not defined in Runnable interface. Interface are like contracts and any concrete class like Thread when it says "I implement Runnable" interface then it must define run() method.
Hope this clears the confusion.
Thanks
Deepak
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic