posted 15 years ago
There are two instances of Thread and two Messengers: (The thread objects overide their run() with that of Messenger)
Synchronization applies to individual instance, inside each instance, execution comes by the order of 1, 2. It is guaranteed. But JVM doesn't guarantee which code segment of specific thread come first and second and so forth. Say, it may execute like:
Thread A, Thread B
-> A, 1st statment
-> put A in a runnable status
-> B, 1st statement
-> B, 2nd statement
-> put B in a runnalbe status
-> A, 2nd statement
-> B, 3rd statement
.....
Therefore, many results of combination. But inside each thread, statements run through by the order of source code.
In this case, whatever combination, Wallace-2 won't come before Wallace-1, and Gromit-1 must come before Gromit-2.