public class Test implements Runnable {
public static void main(String[] args) throws Exception {
String[] ss = {"a", "b"};
for (String s : ss) {
new Thread(new Test(s)).start();
}
}
public void run() {
try {
System.out.println(id);
Thread.sleep(1000);
throw new Exception("异常");
} catch (Exception e) {
idsTemp.put(id , id);
System.out.println("idsTemp.size(): " + idsTemp.size());
}
}
}
Thread.sleep();时间一样基本上都是输出
a
b
idsTemp.size(): 1
idsTemp.size(): 1
要是我这边没有Thread.sleep(); 都输出
a
b
idsTemp.size(): 2
idsTemp.size(): 2
请问要怎么达到下面这样正常的效果
a
b
idsTemp.size(): 1
idsTemp.size(): 2
------解决思路----------------------
idsTemp.加static synchronized(最好这个也加上)