C
Chris Uppal
Joona said:If we have two
threads, one of which does A and B, the other does C and D, then the
actual physical sequence may be A-B-C-D, A-C-B-D, A-C-D-B, C-A-B-D,
C-A-D-B or C-D-A-B. It is always guaranteed that all four operations
are done, A is done before B, and C is done before D. Nothing else
about the ordering is guaranteed. (This is assuming a strictly
sequential intra-thread execution with no control structures.)
I may be missing your point here, but I think that without synchronisation,
there is no such guarantee. As it happens I've just written a passage in
another post which fits just as well here. Apologies to anyone who sees it
twice ;-)
This is discussed in the Java Memory Model JSR, see:
http://www.cs.umd.edu/~pugh/java/memoryModel/PublicReview.pdf
Two threads, two shared variables A and B, two passages of code that access
them without synchronisation:
Initially A == B == 0
Thread 1:
r2 = A;
B = 1;
Thread 2:
r1 = B;
A = 2;
A legal result of this is that r2 == 2 and r1 == 1. The JSR describes this as
"surprising behavoir". Which is a classic bit of understatement...
-- chris