Start() vs Run()

Start() vs Run()

Reading Time: ~ 2 minutes

There is a small detail that often goes by unnoticed (well it did for me!) that does make a huge difference when you want to do some multi-threading work.

As you know (if you don’t check this post), you can create a Runnable which contains a run() method in which, in turn, is where you put your work (a.k.a. code) that you want to be executed on a different thread.

Now you would pack this in a Thread object and which method you are going to call?

The misconception

Thread does contain both the run() and the start() methods. One can think that they are interchangeable since they are both on the Thread class.

That is where the problem lies! I have made this mistake when I was first learning multi-threading and I kept doing because no one pointed it out for me… until they did and I got embarrassed for not knowing this. 😛

Know your stuff

Unfortunately (or fortunately?) these functions are NOT the same. They have a very important difference that may make your project behave completely different of what you’ve expected:

run() : using run() will make the code that you’ve passed on the Runnable to run on the same thread as the one you’ve used to make the method call.

start() : using start() will indeed make the code contained inside the Runnable to be executed on a different thread.

This is of course a huge different since your program would be actually running sequentially on the run() case!!!

Example

Here is a small code so you can test it out for yourself the truth of what I stated above 😉

public class ThreadingTest {

    public static void main(String[] args){
        Runnable r1 = new MyRunnable();
        
        new Thread(r1).start();
        // new Thread(r1).run();

        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("*****************Finished!**********************");
    }

    private static class MyRunnable implements Runnable{
        @Override
        public void run() {
         for (int i = 0; i < 1000; i++) {
                System.out.println(String.format("%03d - My runnable runs a riot!", i));
         }
        }
    }
}

The code is pretty simple: it creates a Runnable which contains inside its run() method, nothing more than a loop printing a sentence to the console.

This Runnable will get wrapped around a thread, with which we call either run or start. The main thread will proceed to wait just 20 milliseconds and will print a stary “Finished”.

Try running this code a couple of times with the start() method call and a couple times with the run() method call. You will see that EVERY TIME you use the run method, the loop will print its 1000 times and AFTER that the Finished will appear. Now on the other hand if you use start, the finished message will be printed somewhere around the loop, showing in this case that the codes are being executed in parallel.

Conclusion

The design of the Thread class allow you to choose at runtime what you’d like to do, and that can be positively leverage. It can also lead to confusion so that why “Knowledge is power”

Leave your opinion, start a discussion, share an experience or just a compliment...

%d bloggers like this: