This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can we only start() a thread? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can we only start() a thread?" Watch "Can we only start() a thread?" New topic
Author

Can we only start() a thread?

Mark Lau
Ranch Hand

Joined: Dec 15, 2001
Posts: 120
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.

Do you people agree?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Mark Lau
Ranch Hand

Joined: Dec 15, 2001
Posts: 120
OK, OK, I gotcha. I was suspecting, though.
So javacertificate is actually right, right?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Yes, the proper way to start a thread is with the start() method.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can we only start() a thread?
 
Similar Threads
Tharead start() /join()
i have question in Thread.. plz help me
Thread output
thread
A question for beginners