• Post Reply Bookmark Topic Watch Topic
  • New Topic

Concurrency Output  RSS feed

 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is from a Whizlabs quiz.
The answer is option D.
I ran this code on 2 separate machines and received an output of 2200.



What could be an output? (Please select 1 option: )
A. 210-1
B. 2100
C. 32100
D. 210
E. 3210-1

Answer: B
Explanation:

The correct answer is: 2100
-------

Because the question asks, "What could be an output?"

a definitive answer is not required and my output of 2200 is the closest to 2100
out of all the options.

Please confirm.


-------
 
Girish Bal
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also getting 2200. Is there any scenario when the output can be 2100?
 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might be a mistake by the author.
One can settle for
option B) since 2200 is the closest number to 2100.
Options A) and E) contain dashes so they can be eliminated.

Thanks for your response.
 
Henry Wong
author
Sheriff
Posts: 22853
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Girish Bal wrote:I am also getting 2200. Is there any scenario when the output can be 2100?


Try ...

1. First thread sets x to three and goes to sleep.
2. Second thread sets x to two and goes to sleep.
3. First thread wakes up, prints the two, and ... sets x to one and goes to sleep.
4. Second thread wakes up, prints the one, and ... setx x to zero and goes to sleep.
5. First thread wakes up, prints the zero, and exits the while loop and method.
6. Second thread wakes up, prints the zero, and exits the while loop and method.

Hope this helps,
Henry
 
Henry Wong
author
Sheriff
Posts: 22853
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:
One can settle for
option B) since 2200 is the closest number to 2100.
Options A) and E) contain dashes so they can be eliminated.


1. Options A) and E) can be eliminated because the AtomicInteger class is thread safe, and for the while loop, as written, it is not possible to "accidentally" pass zero to negative one.

2. Options C) and D) can be eliminated because, in addition to point 1, it is not possible to do any different than four iterations total. Option C) has five iterations and Option D has three iterations, hence, both can be eliminated.

Henry
 
Girish Bal
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds good! Thanks Henry!
 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:

Try ...
1. First thread sets x to three and goes to sleep.
2. Second thread sets x to two and goes to sleep.
3. First thread wakes up, prints the two, and ... sets x to one and goes to sleep.
4. Second thread wakes up, prints the one, and ... setx x to zero and goes to sleep.
5. First thread wakes up, prints the zero, and exits the while loop and method.
6. Second thread wakes up, prints the zero, and exits the while loop and method

I understand how steps 1 through 6 can theoretically generate the number 2100.

Question: How could the above code be altered so that a number of 2100 is generated?
 
Henry Wong
author
Sheriff
Posts: 22853
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:
Question: How could the above code be altered so that a number of 2100 is generated?


The code as written already can generate 2100. You just need to run Java, on a certain operation system, configured in a certain way, on a specific set of hardware, configured in a certain way. And run some other programs that stress the hardware and OS scheduler to schedule those two threads in that way. This is just the nature of race conditions -- it may happens under certain conditions.

On the other hand, by "altered so that a number of 2100 is generated", I assume you mean how to not have the race condition? This is what synchronization (and wait/notify) is for. Of course, you can do this -- but the resultant program will likely look very different.

Henry

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!