• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

THREAD QUESTION OF K&B!! confused

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot vijitha...........!!
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem
 
I got this tall by not having enough crisco in my diet as a kid. This ad looks like it had plenty of shortening:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic