兵法里面讲到一个小例子:
public static Vector<Integer> vector = new Vector<Integer>();
public static void main(String[] args){
for(int i = 0; i<10; i++){
vector.add(i);
}
Thread removeThread = new Thread(new Runnable(){
@Override
public void run(){
int i = 0 ;
while(vector.size()>0){
System.out.println(" vector has " +String.valueOf(vector.size())+ " elements!");
vector.remove(i);
System.out.println("Vector just now has NO." + String.valueOf(i) +" element removed!");
}
}
});
Thread printThread = new Thread(new Runnable(){
@Override
public void run(){
for (int i = 0; i< 10;i++){
System.out.println((vector.get(i)));
}
}
});
removeThread.start();
printThread.start();
}
这里原文是用while(true)来执行的,编译器直接提示下面的代码可以省去,因为永远也执行不到,所以去掉while(true)。