ArrayList不是线程安全的,如果遇到多线程场景,可以通过Collections的synchronizedList方法将其转换成线程安全的容器后再使用。例如像下面这样:
List synchronizedList = Collections.synchronizedList(list);
synchronizedList.add("aaa");
synchronizedList.add("bbb");
for (int i = 0; i < synchronizedList.size(); i++){
System.out.println(synchronizedList.get(i));
}

Was this helpful?

0 / 0

发表回复 0

Your email address will not be published.