ArrayLLis 线程不安全的学习(需要修改)

package 线程安全测试;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

 

class  Output implements Runnable
{
    private ArrayList<Integer> array=null;
    public Output(List<Integer> list)
    {
        array=(ArrayList<Integer>) list;
       
       
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(array.size()>0)
        {
           
            int a=array.remove(0);
            try {
                Thread.sleep((long) (1000*Math.random()));
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"线程取走了数据"+array.size());
           
        }
       
    }
   
   
   
}

public class ArraryListTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<Integer> arry=new ArrayList<Integer>();
        for(int i=0;i<100;i++)
        {
           
            arry.add(i);
        }
       
        Output output=new Output(arry);
    ExecutorService pool=Executors.newFixedThreadPool(10);
    Thread t1=new Thread(output);
    Thread t2=new Thread(output);
    Thread t3=new Thread(output);
    pool.execute(t1);
    pool.execute(t2);
    pool.execute(t3);
    pool.shutdown();
   

    }

}
测试结果

package 线程安全测试;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

 

class  Output implements Runnable
{
    private ArrayList<Integer> array=null;
    public Output(List<Integer> list)
    {
        array=(ArrayList<Integer>) list;
       
       
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(array.size()>0)
        {
           
            int a=array.remove(0);
            try {
                Thread.sleep((long) (1000*Math.random()));
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"线程取走了数据"+array.size());
           
        }
       
    }
   
   
   
}

public class ArraryListTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<Integer> arry=new ArrayList<Integer>();
        for(int i=0;i<100;i++)
        {
           
            arry.add(i);
        }
       
        Output output=new Output(arry);
    ExecutorService pool=Executors.newFixedThreadPool(10);
    Thread t1=new Thread(output);
    Thread t2=new Thread(output);
    Thread t3=new Thread(output);
    pool.execute(t1);
    pool.execute(t2);
    pool.execute(t3);
    pool.shutdown();
   

    }

}

pool-1-thread-3线程取走了数据97
pool-1-thread-2线程取走了数据96
pool-1-thread-3线程取走了数据95
pool-1-thread-1线程取走了数据94
pool-1-thread-1线程取走了数据93
pool-1-thread-1线程取走了数据92
pool-1-thread-3线程取走了数据91
pool-1-thread-2线程取走了数据90
pool-1-thread-2线程取走了数据89
pool-1-thread-3线程取走了数据88
pool-1-thread-2线程取走了数据87
pool-1-thread-1线程取走了数据86
pool-1-thread-1线程取走了数据85
pool-1-thread-3线程取走了数据84
pool-1-thread-2线程取走了数据83
pool-1-thread-3线程取走了数据82
pool-1-thread-2线程取走了数据81
pool-1-thread-1线程取走了数据80
pool-1-thread-2线程取走了数据79
pool-1-thread-3线程取走了数据78
pool-1-thread-2线程取走了数据77
pool-1-thread-1线程取走了数据76
pool-1-thread-2线程取走了数据75
pool-1-thread-3线程取走了数据74
pool-1-thread-3线程取走了数据73
pool-1-thread-1线程取走了数据72
pool-1-thread-2线程取走了数据71
pool-1-thread-3线程取走了数据70
pool-1-thread-2线程取走了数据69
pool-1-thread-1线程取走了数据68
pool-1-thread-2线程取走了数据67
pool-1-thread-1线程取走了数据66
pool-1-thread-1线程取走了数据65
pool-1-thread-3线程取走了数据64
pool-1-thread-2线程取走了数据63
pool-1-thread-3线程取走了数据62
pool-1-thread-1线程取走了数据61
pool-1-thread-1线程取走了数据60
pool-1-thread-1线程取走了数据59
pool-1-thread-3线程取走了数据58
pool-1-thread-2线程取走了数据57
pool-1-thread-2线程取走了数据56
pool-1-thread-2线程取走了数据55
pool-1-thread-1线程取走了数据54
pool-1-thread-2线程取走了数据53
pool-1-thread-2线程取走了数据52
pool-1-thread-1线程取走了数据51
pool-1-thread-3线程取走了数据50
pool-1-thread-1线程取走了数据49
pool-1-thread-2线程取走了数据48
pool-1-thread-2线程取走了数据47
pool-1-thread-3线程取走了数据46
pool-1-thread-1线程取走了数据45
pool-1-thread-3线程取走了数据44
pool-1-thread-2线程取走了数据43
pool-1-thread-3线程取走了数据42
pool-1-thread-1线程取走了数据41
pool-1-thread-1线程取走了数据40
pool-1-thread-2线程取走了数据39
pool-1-thread-3线程取走了数据38
pool-1-thread-3线程取走了数据37
pool-1-thread-2线程取走了数据36
pool-1-thread-1线程取走了数据35
pool-1-thread-3线程取走了数据34
pool-1-thread-2线程取走了数据33
pool-1-thread-1线程取走了数据32
pool-1-thread-2线程取走了数据31
pool-1-thread-2线程取走了数据30
pool-1-thread-3线程取走了数据29
pool-1-thread-2线程取走了数据28
pool-1-thread-1线程取走了数据27
pool-1-thread-3线程取走了数据26
pool-1-thread-1线程取走了数据25
pool-1-thread-2线程取走了数据24
pool-1-thread-2线程取走了数据23
pool-1-thread-3线程取走了数据22
pool-1-thread-3线程取走了数据21
pool-1-thread-3线程取走了数据20
pool-1-thread-2线程取走了数据19
pool-1-thread-1线程取走了数据18
pool-1-thread-3线程取走了数据17
pool-1-thread-3线程取走了数据16
pool-1-thread-2线程取走了数据15
pool-1-thread-2线程取走了数据14
pool-1-thread-2线程取走了数据13
pool-1-thread-2线程取走了数据12
pool-1-thread-3线程取走了数据11
pool-1-thread-1线程取走了数据10
pool-1-thread-1线程取走了数据9
pool-1-thread-1线程取走了数据8
pool-1-thread-2线程取走了数据7
pool-1-thread-3线程取走了数据6
pool-1-thread-3线程取走了数据5
pool-1-thread-3线程取走了数据4
pool-1-thread-1线程取走了数据3
pool-1-thread-2线程取走了数据2
pool-1-thread-1线程取走了数据1
pool-1-thread-3线程取走了数据0
pool-1-thread-1线程取走了数据0
pool-1-thread-2线程取走了数据0

本来只能出现0到99,只出现一次

ArrayLLis 线程不安全的学习(需要修改),布布扣,bubuko.com

ArrayLLis 线程不安全的学习(需要修改)

上一篇:linux-多线程


下一篇:TOMCAT启动提示NB: JAVA_HOME should point to a JDK not a JRE解决