• 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

Threading question

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please explain me why doesn't the run() method be executed after 't.start()'

class Order implements Runnable {
public void run() {
try { Thread.sleep(2000); } catch (Exception e) { }
System.out.print("in ");
}
public static void main(String [] args) {
Thread t = new Thread(new Order());
t.start();
System.out.print("pre ");
try { t.join(); } catch (Exception e) { }
System.out.print("post ");
} }

Which two can result? (Choose two.)

in pre
pre in
in post pre
in pre post
pre in post --- ans when i run the above
pre post in
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"post" is guaranteed to be printd last, because of the call to t.join().

"pre" and "in" can be printed in any order.
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Can you please explain me why doesn't the run() method be executed after 't.start()'


The run method is executed once you called t.start() but has to temporarily suspend for at least 2 seconds before proceeding, because of the call to Thread.sleep(2000)
 
deep rai
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok Thank you Ahmed.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note: Please quote your sources.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic