What is the differences between .run() & .start() in threading?
the only difference to me is that .run() runs straight away. Is that right ?
few.beer.wana.cum.ova?
abhrodip paul
Greenhorn
Joined: Oct 16, 2007
Posts: 27
posted
0
There is some very small but important difference between using start() and run() methods.
Example one:
Thread one = new Thread(); Thread two = new Thread(); one.run(); two.run();
Example two:
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.
Ian Edwards
Ranch Hand
Joined: Aug 14, 2006
Posts: 107
posted
0
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.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
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).