Printable Version Printable Version

Story behind start() and run() methods of Java Thread

Nov 27th, 2009 | By admin | Category: Java

The basic concept of creating a java thread is to extend the class java.lang.Thread or to implement java.lang.Runnable interface. In both the cases , implementation of run() method is mandatory for your thread to be a meaningful Thread.

But , how do you execute a thread successfully ? Take an example of the class below. This is a thread for installation purpose.

Listing 1 : Install Thread

public class InstallThread extends Thread {

	public InstallThread(String threadName){
		super(threadName);
	}

	@Override
	public void run() {
		super.run();
		System.out.println("installing......");
	}

}

The above class does not install anything but gives you an impression of how to create a basic thread. You must have to give an implementation of the run() method.

Have a look at the following class where the instance of the install thread will be created and started.

Listing 2 : Start the Install Thread

public class InstallThreadTest {

	public static void main(String[] args) {
		InstallThread installThread = new InstallThread("Install Thread;");
		installThread.start();
		System.out.println("Installed.");
	}
}

We have started the thread using the start() method of the Thread class rather using the public run() method of the thread we have created.Why it’s only proper to call the start() method to start the thread instead of calling the run() method directly?

Because the run() method is not an ordinary class method. It should only be called by the Java Virtual Machine(JVM). We write threads because we want multi-threading in most of the cases. Writing thread classes is not about a single sequential thread, it’s about the use of multiple threads running at the same time and performing different tasks in a single program.

The JVM needs to work closely with the underneath operating system for the actual implementation of concurrent operations. Improvement in the performance can be achieved by doing so.

You should not invoke the run() method directly. If you call the run() method directly, it will simply execute in the caller’s thread instead of as its own thread. Instead, you need to call the start() method, which schedules the thread with the JVM. The JVM will call the corresponding run() method when the resources and CPU is ready.

Here is a twist. Don’t expect your run method to be executed for all the treads in the same order of start() method execution. The JVM is not guaranteed to call the run() method right way when the start() method is called, or in the order that the start() methods are called.

The JVM must implement a scheduling mechanism that shares the processor among all running threads. This is why when you call the start() methods from more than one thread, the sequence of execution of the corresponding run() methods is random, controlled only by the JVM.

  • Share/Save/Bookmark
Tags: ,

2 comments
Leave a comment »

  1. Hello, I love your blog. This is a great site and I wanted to post a comment to let you know, great job! Thanks Tammy

    Louis Vuitton

    vuitton

  2. This is really a nice forum to get information I need!. The threads here helped me a lot. Thank your guys, and I wish I could do something to help back.

Leave Comment