• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

THREAD QUESTION OF K&B!! confused

 
himanshu kesarwani
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hi ranchers............a few questions in k&b in thread self test are having code like this:

class mythread extends Thread
{
public void run()
{
//this run() is not called
}
}

class abc
{
public static void main(String arg[])
{
mythread t=new mythread(){ //<-i am on getting this brackets role here
public void run()
{
//this run is called
}
};//<-i am on getting this brackets role here

t.start();
}
}



IN my mind these questions are coming:

1.what the work that braket and at the end of the braket ; is used. CAN ANY ONE TELL ME WHAT IS THE BEHAVIOUR OF SUCH BRAKETS.

2.why the run in main is called even the object is of "mythread class"
,and it has overridden the run() so why the main() block's run is executing.

help me out guys!! just one month is left for my exam!!

 
Arka Guhathakurta
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
<-- This actually means you are creating an anonymous class inside the main method. You are defining rather overriding the run method you have declared while defining class mythread. So t.start() will call this run method of anonymous class.However if you want to execute the original run method in your class definition of mythread use super.run()....but I don't know if that will run like a Thread because to run a thread you call start().Please check by experimenting with the code.
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arka Guhathakurta wrote:but I don't know if that will run like a Thread because to run a thread you call start().Please check by experimenting with the code.


Calling run() will execute the code from the same thread as the calling method resides. Call to start will spawn a new thread (thread of execution).
 
himanshu kesarwani
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you arka...!!

but kindly......clarify is this a kind of class defination in that then is this ok
{
int j;
public void run()
{
}
int abc(){
}
};

wat type of these class are do they have there objects even,why thred is taking this inner defination
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The original code(the first post) IS a class.Only thing is that it does not have a name.
But the later code that you gave does not compile. EVEN if the compiler allows you to do that then how can you create a object??
How can you refer the class???
Try tounderstand what is anonymous class are???
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Actually the code "mythread t=new mythread(){........};" doing few things. It's extending the mythread class (hence sub classing) and then override the run method (method happen to be of the mythread class). That code will define a subclass of mythread and instantiate an object and assigned that to a mythread reference t.
 
himanshu kesarwani
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot VIJITHA,

your answer clarified my many doubts....
can i get somewhere about this.........even in k&b its not there....
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inner classes has nothing to do with threads,It's just they have used an anonymous inner class to instantiate a thread object. You will find details under Inner classes topics in K&B.
 
himanshu kesarwani
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot vijitha...........!!
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic