This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I think that we can also directly invoke the run method of the thread like so: myThread.run(); After all, the start method also simply calls run(), right? But look at this. This problem is from www.javacertificate.com
Which of the following statements are true? [Check all correct answers] 1 A thread is a different program. 2 A thread can share data with other threads. 3 A thread is started by calling the start method. 4 A thread is started by calling the run method.
javacertificate says only 2 and 3 are correct. I think 2 and 3 and 4 are all correct. This is what javacertificate.com says:
The only correct way to start a thread is calling the start method of the Thread class.
Why do you think that the start() method only executes the run() method? Did you actually take a look at the ocde that the start() method invokes? Here is the code: public synchronized native void start(); So there is obviously something else going on than just executing the run() method. In fact, there is a lot more going on. If you just run the run() method it does not create a separate thread. Only when you run the start() method does it create a separate thread.