java内存模型那章的例子需要自己改下_深入理解Java虚拟机书评-查字典图书网
查字典图书网
当前位置: 查字典 > 图书网 > 编程 > 深入理解Java虚拟机 > java内存模型那章的例子需要自己改下
Lenis 深入理解Java虚拟机 的书评 发表时间:2016-05-16 06:05:12

java内存模型那章的例子需要自己改下

兵法里面讲到一个小例子:

        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)。

展开全文
有用 0 无用 0

您对该书评有什么想说的?

发 表

推荐文章

猜你喜欢

附近的人在看

推荐阅读

拓展阅读

对“java内存模型那章的例子需要自己改下”的回应

Lenis 2016-05-16 06:41:39

勘误:“兵法”是并发的意思。