• 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

Threads example execution flow

 
Ranch Hand
Posts: 48
MicroProfile Quarkus Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, could any one drop some light at this main method output and flow execution:

It is currently printing:

While I was expecting:
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As an experiment, change the 1000 and 2000 numbers around and see what happens.
 
Marouane Trabelsi
Ranch Hand
Posts: 48
MicroProfile Quarkus Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It prints the following:


Any explanations please?
 
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
In principle, the order in which threads are scheduled is unpredictable. So if you have two threads printing things at the same time, then it is undetermined what gets printed first.

Why did you expect the output to be "Started", "end" and then "ran"? Look at what your program is doing:

- Line 20: Creates a new Thread
- Line 21: Starts the new Thread
- Line 22: Prints "Started"
- Line 25: Wait for 2 seconds. In the meantime, the thread is on line 12, waiting for 1 second.
- Line 17: After 1 second, the thread comes out of the waiting period and prints "ran".
- Line 31: After 2 seconds, the main thread comes out of the waiting period (from line 25) and prints "end"
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 25 worker.sleep(2000); might be misleading.
Notice that sleep(long) method in Thread class is static.
This means that worker.sleep(2000) is exactly the same as Thread.sleep(2000) which sleeps current thread (which is main thread), not worker thread.
 
Ranch Hand
Posts: 133
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will get different results depending on the sleep intervals you keep.

Also, the position of

Is there any specific check you are intending to do here?
 
Marouane Trabelsi
Ranch Hand
Posts: 48
MicroProfile Quarkus Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Jesper de Jong thank you for the in depth explanation of the instructions flow. An to answer your question:

Why did you expect the output to be "Started", "end" and then "ran"?


The answer is just in what @Paweł Baczyński has stated. I was mislead by the sleep() method. I though that the worker.sleep(2000) would force the worker thread to sleep for the amount of 2s thus 1s plus the one under the run() method body:
  • Line 20: Creates a new Thread
  • Line 21: Starts the new Thread
  • Line 22: Prints "Started"
  • Line 25: Thread sleeps for 2 seconds. In the meantime, the thread is on line 12, waiting for 1 second ==> wait for 2s in total.
  • Line 31: The main thread prints "end".
  • Line 17: After 2 second, the thread comes out of the waiting period and prints "ran".


  • @rohit chavan there is no relevant purpose of using the interrupt() method, just trying to demonstrate Thread power.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic