CodesJava

Easy learning with example program codes

Can we call run method directly?


Yes, we can call run method directly. Only difference is that when start method is called it creates a separate call stack for that thread but in case when run method is called directly from main method it will not create a new call stack. Run method is goes into current call stack. i.e. No new thread will be created and it is the responsibility of the main thread to complete the job.

Example:

ThreadExample.java

/**
 * This program is used to show that 
 * we can call run method directly.
 * @author codesjava
 */
class Test extends Thread{
	public void run(){
		System.out.println("thread started.");
	}
}
 
public class ThreadExample {
	public static void main(String args[]){
		//creating thread.
		Test thrd = new Test();
 
		//call run method.
		thrd.run();
	}
}

Output:

thread started.

Download this example.
 
Next Topic: Difference between Thread.yield() and Thread.sleep() methods.
Previous Topic: Can we start a thread twice?

Please follow and like us:


Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki