notanormie
notanormie

Reputation: 455

Multi thread updating value java

We have 2 threads:

Their task is:

At start X contains value 0. Both threads modify value at same location.

Both threads start at the same time and do 1000 iterations.

Question: What is the smallest value X after both threads finish? (not 1000 and not 2000)

Upvotes: 2

Views: 109

Answers (1)

richj
richj

Reputation: 7529

The smallest possible number is 2.

T1 reads the value 0.
T2 increments X 999 times to 999.
T1 writes the value 1
T2 reads the value 1
T1 increments X 999 times to 1000
T2 writes the value 2.

Upvotes: 8

Related Questions