• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

threads

 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Khalid Mughal 3rd edition , question number 13.6




can someone explain me the control flow of threads using this above code ?
output is : | R2 | R2 | R1b |
 
author & internet detective
Posts: 41415
854
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, note that output isn't guaranteed. You could get either of:
|R2||R1b||R2|
|R2||R2||R1b|

Notice how R2 is in their twice. That's because run() doesn't start a new thread. This means that both the first and third statements in run() are in R2 and the middle one is in R1. The reason the order changes is R1 and the final println could run in either order.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote: The reason the order changes is R1 and the final println could run in either order.


could you please elaborate this, I didn't got the reason ?

actually I am getting the output the one I have written above (at every run), no change is there ?
for me |R2||R1b||R2| is not the issue of doubt but |R2|R2||R1b| is.... because according to the code |R2| is the first to be print !!
 
Jeanne Boyarsky
author & internet detective
Posts: 41415
854
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:actually I am getting the output the one I have written above (at every run), no change is there ?


It depends on your computer and a number of things. The point is that it isn't guaranteed. And on the exam, you have to understand what is guaranteed vs what can happen.

Puspender Tanwar wrote:could you please elaborate this, I didn't got the reason ?


Look at what runs in the foreground vs background.
1) The main method starts a thread in the background
2) There are no other threads so R2 starts running. The first thing R2 does is call run() on R1. Since run() doesn't start a new thread, this synchronously prints R2
3) Next R2 starts a new thread (R1b). Since it is being run in the background, either R1b can be printed immediately or the next line can print R2

 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Jeanne Boyarsky
 
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic