What is the differences between .run() & .start() in threading?
the only difference to me is that .run() runs straight away. Is that right ?
Joined: Oct 16, 2007
There is some very small but important difference between using start() and run() methods.
Thread one = new Thread(); Thread two = new Thread(); one.run(); two.run();
Thread one = new Thread(); Thread two = new Thread(); one.start(); two.start();
The result of running examples will be different.
In Example one the threads will run sequentially: first, thread number one runs, when it exits the thread number two starts.
In Example two both threads start and run simultaneously.
because the start() method call run() method asynchronously (does not wait for any result, just fire up an action), while we run run() method synchronously - wait when it quits and only then can run the next line of our code.
Joined: Aug 14, 2006
You can indeed call the run() method like any other method - but the intended use from the point of threading is that you call the start() method to start the thread and then the thread will execute your run() method.
When you call the run() method directly it is not running in a new thread.
Joined: Mar 22, 2005
Welcome to JavaRanch.
The important difference is that only calling start() will actually cause the code to run in a new thread. Calling run() will cause the code in to be run the current thread (meaning no new thread is started, thereby defeating the purpose of using Thread in the first place).