A first example in Schildt's
Java: A Beginner's Guide displays what synchronized does (and a little of what it doesn't, too.) He then says to remove the
word synchronized to see what happens. I typed in the example (with minor changes):
With synchronized:
Without synchronized:
Each
thread adds to 15 separately, but without synchronized they only add to 29. Why?
Also, how can the second thread start first? I've seen that a few times already, both in this example and the previous one (not shown in this post.)