public class UnsafeCol implements Runnable{
List<String> list = new ArrayList<>();
@Override
public void run() {
synchronized (list) {
list.add(Thread.currentThread().getName());
}
}
public static void main(String[] args) {
UnsafeCol r = new UnsafeCol();
for (int i = 0; i < 100; i++) {
new Thread(r).start();
}
System.out.println(r.list.size());
}
}
output: 84
description: I always get the incorrect size, but I have used synchronized to lock the list,why?