Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Concurrency Output

Thomas Hauck
Ranch Hand
Posts: 95
The following is from a Whizlabs quiz.
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

Explanation:

-------

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.

-------

Girish Bal
Ranch Hand
Posts: 77
I am also getting 2200. Is there any scenario when the output can be 2100?

Thomas Hauck
Ranch Hand
Posts: 95
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.

Henry Wong
author
Marshal
Posts: 21732
85
• 2
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
Marshal
Posts: 21732
85
• 1
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
Sounds good! Thanks Henry!

Thomas Hauck
Ranch Hand
Posts: 95
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
Marshal
Posts: 21732
85
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