这里每一次线程抢占到CPU都运行run()方法,run方法new了一个ArrayList对象,按道理说每一次新new一个对象都会在堆里面新建一个,为什么到最后这些值都加到了同一个ArrayList对象啊。
public void run() {
ArrayList<Integer> boxList = new ArrayList<>();
//1.循环
//2.同步代码块
//3.判断(共享数据到末尾)
//4.判断(共享数据没有到末尾)
while(true){
synchronized (Thread.class){
Collections.shuffle(list);
int prize = list.remove(0);
boxList.add(prize);
}
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}