• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Why the overridden start() of this thread never called?

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's is the expected result? (choose 2)

class Thread1 extends Thread{
public void start(){ super.start();}
public void run() {
while(true)
System.out.println("Running");
}

static public void main(String...a){
Thread1 anObj=new Thread1();
anObj.start();
}
}

A) The start() method in overriden thread is called first
B) The start() method of the Thread class is called
c) Choice B is true so the run() method causes an infinite loop
D) Choise A is true so the run() method of the super class runs and nothing is printed
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
B and C are true.

Since u r overriding start() method. So ur start method will be called first not overridden method.

This start() method calls overridden method defined in Thread class.

It prints Running in infinite loop....


regards


Naseem
[ June 11, 2006: Message edited by: Naseem Khan ]
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This start() method calls overridden method defined in Thread class.

It prints Running in infinite loop....



The run() method in Thread class has an empty body. According to K&B it does nothing. So when you said above it "calls overridden method defined in Thread class", I thought it would call the empty run() and so nothing is printed, right?
[ June 11, 2006: Message edited by: Firas Zureikat ]
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. First ur start() method is called.

2. This calls start() method of Thread class coz of super.start();

3. start() method of thread class calls start0() method of Thread class which is native and private.

4. This start0() method calls run() method. Which run() method will be called. Urs or Thread class??

Since ur class extends Thread class and u r overriding run() method in ur class, the ur run() method will be called. If u don't override, then inherited run() method will be called. In that case, there will be no output.

This is the reason wen ur class extends Thread class, then u must override run() method, otherwise inherited run() method will be called which will do nothin.

regards

Naseem Khan
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, thanks...So the overridden run() is called as usual...Also I noticed if I comment out super.start() nothing is printed out.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if u comment super.start(), nothin will happen. Even a seperate thread will not start.

start() method of thread class calls a native method which actually does each and everything.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic