SynchronizationExample -- comparing
unsynchronized and synchronized behaviors.
This applet runs a simple loop that would be quite robust in a
single-threaded world. But by running the same loop in multiple
threads, you render the loop unsafe. Adding synchronization gives
you back safety, but with a significant performance penalty.
Experiment:
- Run the loop unsynchronized a few times. Note how many
null pointer exceptions you see on average and how long
the two million loops (2 threads times 1 million loops
per thread) take.
- Now try the same loops synchronized, but be prepared to
wait a while (hint: up to several minutes).
The source.