• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads Behavior of run and Start methods.

 
nitin goswami
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q.What would be the output???
class MyThread extends Thread {

public void run() {
System.out.println("MyThread:run()");
System.out.println("Active Threads"+Thread.activeCount());
}

public void start(){
System.out.println("MyThread:start()");
}
}

public class MyRunnable implements Runnable {
public void run() {
System.out.println("MyRunnable:run()");
System.out.println("Active Threads "+Thread.activeCount());

}
public void start(){
System.out.println("MyRunnable:start()");
}
public static void main(String args[]){
//Creating two thread Objects
MyThread myThread= new MyThread();
MyRunnable myRunnable= new MyRunnable();

//Starting Two thread of Execution.
myThread.start(); //***Line #
new Thread(myRunnable).start();

}
}
Ans.MyThread:start()
MyRunnable:run()

Now comes my Questions 1)Why not Executed the run of MyThread class?
2)Active Thread Count 3?if we do not start myThread(if we comment /*** Line #),just create an object of MyThread class ,it gets Counted as a Active Thread.
Pl explain.thx
 
Steven Bell
Ranch Hand
Posts: 1071
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the first question, you have overridden the Thread start method in your MyThread class so it no longer calls the run method.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic