Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Enthuware toughest question doubt

 
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Question from enthuware (q:23)..

(a) Numbers 1 to 10 will be printed (none repeated or missed) in a serial order.
(b) Numbers 1 to 10 will be printed (none repeated or missed) in any order.
(c) Total of 10 numbers will be printed
(d) The final value of threadcounter just before the program terminates will be 10.
(e) The final value of threadcounter just before the program terminates may be less than 10.

Answer is c and e

I understood c but how can final value of threadCounter be less than 10..
Simple logic says variable is incremented 10 times may be in indefinite manner ..

Can some one explain this??
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about race conditions in a multi-threaded environment when multiple threads are trying to modify a single resource - in this case threadcounter. The addition operation in line 6 though a single line java code cannot be taken as an atomic operation. It could involve several steps and we can't assume all the steps will be performed in one single run without pause. For example it could happen that a running thread loads threadcounter variable value on its own stack in order to carry out the addition operation but just before it carries out the addition it goes into runnable state. Then another thread comes along and loads the same threadcounter variable value into its own stack (before the first thread having a chance to update it). Now both threads incremement the same value, and update the static variable back on the heap. In this case the threadcounter value will be less than what we intended.
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah i agree with you increment operation is in fact divided into several steps ...
but i think that those things (that appears atomic) were taken care by JVM itself..

If it is not the case then final value must be 10 ??
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

my initial analysis was wrong and i think the answer e should also be wrong. when i ran the program the
number last printed was not consistent. sometimes it prints 9, 6, 7, 10. since it can't be determined the
order in which the thread will print the values.
 
Shaikh Ali
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question asks for the final value of threadcounter variable not the last value printed on screen.
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main thread creates ten threads in the for loop itself. Their running order is not guranteed, while one is inside the run method some other thread(s) get a chance to run and finish so you will see unordered values printed. The final value would be 10 but the printed order may vary...
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So we conclude that final value of the threadCounter is 10 ,
ans c,d are the right answers....
 
Shaikh Ali
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot assume ++ is an atomic operation.
 
Enthuware Software Support
Posts: 4646
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
The following explanation is already provided with option 4:


This is not correct because a post fix operation is not an atomic operation. It can be thought of as:

int temp = threadcounter;
threadcounter = threadcounter + 1;
return temp;

So it is possible for two threads to increment the value simultaneously and thereby causing it to increment the value only once. So the final value may be less than 10.



thank you,
Paul.
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Paul ...
 
You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic