public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
ArrayList<String> strings = new ArrayList<String>();
strings.add("0");
for (int i = 0; i < strings.size(); i++) {
if (strings.get(i).equals("" + i)) {
try {
Thread.sleep(500);
} catch (Exception e) {
// TODO: handle exception
}
int next = i + 1;
System.out.println("next = " + next);
strings.add(next, next + "");
System.out.println("add " + next);
}
}
}
}).start();
new Thread(new Runnable() {
public void run() {
ArrayList<String> strings = new ArrayList<String>();
strings.add("0");
for (int i = 0; i < strings.size(); i++) {
if (strings.get(i).equals("" + i)) {
try {
Thread.sleep(500);
} catch (Exception e) {
// TODO: handle exception
}
int next = i + 1;
System.out.println("next = " + next);
strings.add(next, next + "");
System.out.println("add " + next);
}
}
}
}).start();
}
以上,如果与当前的相同,则添加
之前遇到类似的问题,以为只会有一次循环,结果写个测试类,发现这居然无限循环了
到处查了查发现,这样for循环,每执行一次,都会调用一次getsize,导致一直循环下去