I am new to Java and I’m attending a Concurrent Programming course. I am desperately trying to get a minimal working example that can help to demonstrate concepts I have learnt like using ‘synchronized’ keyword and sharing an object across threads.
Have been searching, but could not get a basic framework. Java programmers, kindly help.
I am new to Java and I’m attending a Concurrent Programming course. I am
Share
Here is a very shot example of sharing an array between two threads. Usually you will see all zeros, but sometimes things get screwy and you see other numbers.
But, if you synchronize on
arraround the looping you will always see all0s in the print out. If you uncomment the synchronized block, the code will run without error.