The member variable index is a multi-thread shared variable. You need to add volatile to ensure the visibility of this variable in multi-threads. The declaration code is changed to the following:
private volatile int index = 0;
Specifically why please learn the basic knowledge related to java concurrent programming
I think this method is better.
java8 parallel stream
The member variable index is a multi-thread shared variable. You need to add volatile to ensure the visibility of this variable in multi-threads. The declaration code is changed to the following:
Specifically why please learn the basic knowledge related to java concurrent programming
It’s best to use thread synchronization in your run()