GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes THREAD QUESTION OF K&B!! confused 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 "THREAD QUESTION OF K&B!! confused" Watch "THREAD QUESTION OF K&B!! confused" New topic
Author

THREAD QUESTION OF K&B!! confused

himanshu kesarwani
Ranch Hand

Joined: Aug 27, 2008
Posts: 84

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!!


SCJP 5
Arka Guhathakurta
Ranch Hand

Joined: Mar 01, 2009
Posts: 46
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.


regards,
Arka
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3833

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).


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
himanshu kesarwani
Ranch Hand

Joined: Aug 27, 2008
Posts: 84
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

Joined: Aug 29, 2008
Posts: 580

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???


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3833




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

Joined: Aug 27, 2008
Posts: 84
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

Joined: Mar 24, 2008
Posts: 3833

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

Joined: Aug 27, 2008
Posts: 84
thanks a lot vijitha...........!!
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3833

No problem
 
GeeCON Prague 2014
 
subject: THREAD QUESTION OF K&B!! confused