GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
What is the method used to schedule a thread for execution?
The given answer(from Boone) is public void run() {}
Is it not start()?


Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Yes, you should use the start method to create a new thread and get it scheduled to run. Invoking the run method explicitly will not create a new thread.
Corey


SCJP Tipline, etc.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Originally posted by Corey McGlone:
Yes, you should use the start method to create a new thread and get it scheduled to run. Invoking the run method explicitly will not create a new thread.

... hmmm calling .start() doesn't actually create a new thread right??? you still have to do that with the new operator?
calling .start() just makes it elligible to run, right?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Invoking the start method on a thread makes a thread active. Of course, as start is an instance method, you must have created an instance of a Thread object (which is really just a convenient handle to the underlying thread) in order to invoke it.
From the JLS, §17.12 Threads:

Creating a Thread object creates a thread and that is the only way to create a thread. When the thread is created, it is not yet active; it begins to run when its start method is called.

I hope I didn't confuse anyone.
Corey
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
I understand the start and run method behaviour. So, what is the answer? start() and run() declaration?
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055

What is the method used to schedule a thread for execution?

I think this question is vague, and unfairly so. run() is used to define what is executed, start() is used to the start the ball rolling, and setPriority() is used to "suggest" to the scheduler to give it preferential status (or conversely).
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Thiru Thangavelu:
So, what is the answer?

I would answer start().
 
GeeCON Prague 2014
 
subject: Thread