public static void main(String[] args) throws InterruptedException{
List<Integer> list = new ArrayList<>();
Runnable runnable = () -> {
for (int i = 0; i < 50000; i++) {
list.add(i);
}
};
// 创建两个线程
for (int i = 0; i < 2; i++) {
new Thread(runnable).start();
}
Thread.sleep(5000);
System.out.println(list.size());
}
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
*/
public boolean add(E e) {
synchronized (lock) {
Object[] es = getArray();
int len = es.length;
es = Arrays.copyOf(es, len + 1);
es[len] = e;
setArray(es);
return true;
}
}