Friday, March 24, 2017

Why do one call start method of thread if start() calls run() in turn" or "What is difference by calling start() over run()method in java thread" are two widely popular beginner level multi-threading interview question. When a Java programmer start learning Thread, first thing he learns is to implement thread either overriding run() method of Thread class or implementing Runnable interface and than calling start() method on thread, but with some experience he finds that start() method calls run() method internally either by looking API documentation or just poking around, but many of us just don’t care at that time until its been asked in Java Interview. In this Java tutorial we will see What is difference between calling start() method and run() method for starting Thread in Java.

Difference between start and run in Java Thread

So what is difference between start and run method? Main difference is that when program calls start() method a new Thread is created and code inside run() method is executed in new Thread while if you call run() method directly no new Thread is created and code inside run() will execute on current Thread. Most of the time calling run() is bug or programming mistake because caller has intention of calling start() to create new thread and this error can be detect by many static code coverage tools like findbugs. If you want to perform time consuming task than always call start() method otherwise your main thread will stuck while performing time consuming task if you call run() method directly. Another difference between start vs run in Java thread is that you can not call start() method twice on thread object. once started, second call of start() will throw IllegalStateException in Java while you can call run() method twice.

Code Example of start vs run method

Here is a simple code example which prints name of Thread which executes run() method of Runnable task. Its clear that if you call start() method a new Thread executes Runnable task while if you directly call run() method task, current thread which is main in this case will execute the task.

Caller: start and code on this Thread is executed by : Thread-0
Caller: run and code on this Thread is executed by : main

In Summary only difference between start() and run() method in Thread is that start creates new thread while run doesn't create any thread and simply execute in current thread like a normal method call.

4 comments
:

Anonymous
said...

In most cases a direct call to a Thread object's run() method is a bug. The programmer wnated to start a new thread for parallel execution, but accidentally called run() instead of start(), so the run() method will execute in the caller's thread of control. By the way its easy to detect bug, if you are using static code analyzer like Findbugs or HP Fortify. Almost all code coverage and static analysis tools has rules to detect such bugs during development. In many firms fortify scanning of new code is mandatory.

once started, second call of start() will throw IllegalStateException in Java while you can call run() method twice.Here it JVM will throw IllegalThredStateException. i.e. The thread is not in the valid state for the operation which user is trying to perform.