• August 29, 2014
  • Core Java
  • Comments Off on Can we call run method directly?

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.

Example:

ThreadExample.java

/**
 * This program is used to show that 
 * we can call run method directly.
 * @author javawithease
 */
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?

Related Topics:

Multithreading in java.
Thread life cycle in java.
Way of creating thread in java.
Commonly used methods of Thread class.
Thread scheduling in java with example.
Thread priority in java with example.
Naming a thread in java with example.
Joining a thread in java with example.
Daemon thread in java with example.
Can we start a thread twice?
Can we call run method directly?
Difference between Thread.yield() and Thread.sleep() methods.
Deadlock in java with example.
Starvation in java with example.
Inter-thread communication in java with example.
Synchronization in java with example.
Synchronized method in java with example.
Static synchronization in java with example.
Synchronized block in java with example.

 


Comments are closed.