Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread

 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the method used to schedule a thread for execution?
The given answer(from Boone) is public void run() {}
Is it not start()?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand the start and run method behaviour. So, what is the answer? start() and run() declaration?
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thiru Thangavelu:
So, what is the answer?

I would answer start().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic