Why do we need start() method in Thread class? In Java API description for Thread class is written : "Java Virtual Machine calls the run method of this thread..".
Couldn't we call method run() ourselves, without doing double call: first we call start() method which calls run() method? What is a meaning to do things such complicate?
There is some very small but important difference between using start() and run() methods. Look at two examples below:
Example one:
| Code: |
Thread one = new Thread();
Thread two = new Thread();
one.run();
two.run();
|
Example two:
| Code: |
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.
Conclusion: the start() method call run() method asynchronously (does not wait for any result, just fire up an action), while we run run() method synchronously - we wait when it quits and only then we can run the next line of our code.
Disclaimer: English is not native language for me and I appreciate if you correct my errors in a friendly way
1148 bytes more | comments? | | Score: 0
|