- import java.util.Vector;
- public class Theader {
- /**
- * @param args
- */
- public static void main(String[] args) {
- /*Runnable r =new Test();
- new Thread(r).start();
- new Thread(r).start();*/
-
- Runnable r2 =new Test();
- new Thread(r2).start();
- new Thread(r2).start();
- }
- }
- class Test2 implements Runnable{
- String s="123456789";
- Vector<String> v=new Vector<String>();
-
- public Test2(){
- for(char c : s.toCharArray()){
- v.add(String.valueOf(c));
- }
-
- }
-
- @Override
- public void run() {
- while(!v.isEmpty()){
- System.out.println(Thread.currentThread().getName()+":"+v.get(0));
- v.remove(0);
-
- }
- }
-
- }
复制代码 Thread-0:1
Thread-0:2
Thread-0:3
Thread-0:4
Thread-0:5
Thread-0:6
Thread-0:7
Thread-0:8
Thread-0:9
Thread-1:1
Exception in thread "Thread-1" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.remove(ArrayList.java:387)
at com.heimait.day20121015.Test.run(Theader2.java:22)
at java.lang.Thread.run(Thread.java:619)
不是说 vector 线程安全吗?
vector 线程安全 怎么理解?? |