• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Every1,
I made these two programs for Threads...one extends the Thread class and the other implements the Runnable interface.

Both the programs work fine but theres 1 thing I could not understand :







Program-1 prints the name of the thread as ThreadClass while Program-2 prints java.lang.Thread. I was expecting it to print RunnableThreadClass !!

Why's that so ?

Please help. What should I do so that it prints RunnableThreadClass ?

Thanks
 
Arul Prasad
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi getClass() is the method of Object class which returns the class name
in ur second prog u r saying

here object t is belongs to Thread class thats why ur getting the java.lang.Thread.and the other point is ur RunnableThreadClass was implementing the Runnable interface it is not a Type of Thread ....



in your first prog ur calling simply

getClass() -> which is nothing but this.getClass() inside the class
ThreadClass
thats why ur getting the output as ThreadClass...

hope u got clear
 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No I did not understand !! Please clarify further.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Referring to your second example: t is a Thread object. You created it in your constructor.

On the other hand, in the run() method, this is a reference to the Runnable object not the Thread object. That is, this is a reference to an instance of your RunnableMethodClass.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic