Java???????????????
???????????? ???????[ 2014/10/15 11:07:55 ] ????????Java ??????
??????δ????????????????????????
??????????????????????Vector??????ArrayList????????
????????????е?????????????????????
????????????Vector?е?add??????get????????????????????????ж???????з?????????????????????ж??????????????????????????????ж?????????????????????Щ????????????????
???????????????????????????????Java 1.5???????????????λ??java.util.concurrent???£?????????????????????????????н?????
????2.??????????????????
??????????????Vector?е??????????????????????????????????????????????????????????δ???
public class Test {
static Vector<Integer> vector = new Vector<Integer>();
public static void main(String[] args) throws InterruptedException {
while(true) {
for(int i=0;i<10;i++)
vector.add(i);
Thread thread1 = new Thread(){
public void run() {
for(int i=0;i<vector.size();i++)
vector.remove(i);
};
};
Thread thread2 = new Thread(){
public void run() {
for(int i=0;i<vector.size();i++)
vector.get(i);
};
};
thread1.start();
thread2.start();
while(Thread.activeCount()>10) {
}
}
}
}
????????????????е?????
?????????????????????δ?????????????±???硣
?????????????????Vector????????????????????????????????Vector??????????????????????????????????????????????????
?????????????????????????????
????for(int i=0;i<vector.size();i++)
????vector.get(i);
??????????vector??size???????????10??i????9
?????????????????????????
????for(int i=0;i<vector.size();i++)
????vector.remove(i);
???????±??9???????????
??????????get?????????±??9?????????????????
???????????????????????????????????????????????????????????
public class Test {
static Vector<Integer> vector = new Vector<Integer>();
public static void main(String[] args) throws InterruptedException {
while(true) {
for(int i=0;i<10;i++)
vector.add(i);
Thread thread1 = new Thread(){
public void run() {
synchronized (Test.class) { //???ж???????
for(int i=0;i<vector.size();i++)
vector.remove(i);
}
};
};
Thread thread2 = new Thread(){
public void run() {
synchronized (Test.class) {
for(int i=0;i<vector.size();i++)
vector.get(i);
}
};
};
thread1.start();
thread2.start();
while(Thread.activeCount()>10) {
}
}
}
}
????3. ConcurrentModificationException??
???????Vector??????????????е???????????ConcurrentModificationException???????????????????????????н?????
??????????????????в?????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11