Saturday, August 30, 2014

Difference between start and run in Java Thread

Difference between start and run in Java Thread
This is one of the popular interview question and important to know the difference between these two for multi thread programming.  Why do one call                   
start method of thread if start() calls internally to run() in

So what difference is between start and run method? The main difference between there are –

    1. While 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.
    2. Another difference 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.
While java programmer implemented threading in java most the time his/her intention is to call start() method to create new thread but here calling run() is bug or programming mistake. This error can be detected by many static code coverage tools like FindBugs.

FindBugs is a program which uses static analysis to look for bugs in Java code, it is free software.

No comments:

Post a Comment