wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Behavior of run and Start methods. 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 "Threads Behavior of run and Start methods." Watch "Threads Behavior of run and Start methods." New topic
Author

Threads Behavior of run and Start methods.

nitin goswami
Greenhorn

Joined: Mar 01, 2005
Posts: 25
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

Joined: Dec 29, 2004
Posts: 1071
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.
 
subject: Threads Behavior of run and Start methods.
 
Similar Threads
Thread
Thread Question---start()
Overriding the start method of Thread Class
Whizlabs Thread Question
Thread Doubt