运行了以后等了好久,500个线程也执行不完是为什么?
package synchronize; public class VolatileDemo { private volatile int number = 0; public int getNumber() { return this.number; } public void increase() { this.number++; } public static void main(String[] args) { final VolatileDemo vd = new VolatileDemo(); for (int i = 0; i < 500; i++) { new Thread(() -> vd.increase()).start(); } while (Thread.activeCount() > 1) { Thread.yield(); } System.out.println("number is " + vd.getNumber()); } }
这个代码在idea里面启动了以后,一直不结束是为什么?