import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;
public class MT {
/**
* @param
*/
private static Vector v = new Vector();
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
PutThread p = new PutThread(v);
Thread t = new Thread(p);
GetThread g = new GetThread(v);
GetThread g2 = new GetThread(v);
GetThread g3 = new GetThread(v);
t.start();
t.sleep(10);
g.start();
g2.start();
g3.start();
}
}
class PutThread implements Runnable {
private Vector vv;
public PutThread(Vector v) {
this.vv = v;
}
public void run() {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(
"d:\\a.txt")));
String tmp = "";
while ((tmp = br.readLine()) != null) {
System.out.println("before put-----" + vv.size());
vv.add(tmp);
System.out.println("after put-----" + vv.size());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class GetThread extends Thread {
private Vector vv;
public GetThread(Vector v) {
this.vv = v;
}
@Override
public void run() {
while (!vv.isEmpty()) {
System.out.println(Thread.currentThread().getName()
+ "===before get=====" + vv.size());
try {
Object o = vv.remove(0);
this.sleep(10);
} catch (Exception e) {
System.out.println(Thread.currentThread().getName()
+ " ****************crashed****************");
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()
+ "===after get=====" + vv.size());
}
}
}
其中a.txt是一个约有5万行的文本
执行的时候,会抛
...
before put-----5
Thread-2===before get=====3
Thread-1===after get=====3
Thread-0===before get=====4
Thread-2===after get=====3
Thread-1===before get=====3
after put-----4
Thread-1===after get=====1
Thread-2===before get=====2
Thread-0===after get=====2
Thread-2===after get=====0
Thread-1===before get=====1
before put-----1
after put-----1
Thread-1 ****************crashed****************
before put-----1
after put-----2
before put-----2
after put-----3
before put-----3
after put-----4
before put-----4
after put-----5
java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
before put-----5
after put-----6
before put-----6 at java.util.Vector.remove(Vector.java:777)
at GetThread.run(GetThread.java:17)
after put-----7
Thread-1===after get=====7
before put-----7
Thread-1===before get=====7
after put-----8
Thread-1===after get=====7
before put-----7
Thread-1===before get=====7
after put-----8
Thread-1===after get=====7
before put-----7
Thread-1===before get=====7
after put-----8
Thread-1===after get=====7
before put-----7
Thread-1===before get=====7
after put-----8
Thread-1===after get=====7
before put-----7
Thread-1===before get=====7
after put-----8
Thread-1===after get=====7
before put-----7
Thread-1===before get=====7
after put-----8
...
Vector不是线程安全的吗,为什么会抛异常呢?
crashed之后,为什么后面的进程还有 Thread-1 呢?
java 多线程 Vectoer Exception
------解决方案--------------------
线程安全也是相对的
while (!vv.isEmpty()) 判断和下面的remove动作不是原子操作,也就是没有共享锁。
你可以这样理解:
Thread1 while (!vv.isEmpty()) true 。进去后,正好Thread2 也进去了,并且先于Thread1 Remove了