• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand the start and run method behaviour. So, what is the answer? start() and run() declaration?
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thiru Thangavelu:
So, what is the answer?


I would answer start().
 
That feels good. Thanks. Here's a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic