Java 集合线程安全

线程不安全的的集合有(HashSet,TreeSet,ArrayList,ArrayDeque,LinkedList,HashMap,TreeMap);

线程安全的集合有(Vector,HashTable);

Java给你的保证的线程安全,是说它的方法是执行是排它的,而不是对这个对象本身的多次调用情况下,还是安全的。

1)线程不安全案例:

public class ListThread implements Runnable {  
    String name;  
    List l; 
  
    ListThread(String name, List l) {  
        this.name = name;  
        this.l = l;  
    }  
  
    public void run() {  
        System.out.println(name + "start");  
        for(int i = 0; i<6; i++){
            l.add(name + ".add");  
            System.out.println(name + " list size is " + l.size());  
            try {  
                Thread.sleep(10);  
            } catch(InterruptedException e) {  
                System.out.println(e.getMessage());  
            }  
        }  
    }  
  
    public static void main(String args[]) throws InterruptedException {  
  
        List l = new ArrayList();  
  
        ListThread hello1 = new ListThread("hello1", l);  
        ListThread hello2 = new ListThread("hello2", l);  
        ListThread hello3 = new ListThread("hello3", l);  
  
        Thread h1 = new Thread(hello1);  
        Thread h2 = new Thread(hello2);  
        Thread h3 = new Thread(hello3);  
        h1.start();  
        h2.start();  
        h3.start();  
    }  
}  

执行结果:

hello1start
hello1 list size is 1
hello3start
hello3 list size is 2
hello2start
hello2 list size is 3
hello1 list size is 4
hello3 list size is 5
hello2 list size is 6
hello1 list size is 7
hello2 list size is 9
hello3 list size is 9
hello1 list size is 10
hello3 list size is 11
hello2 list size is 12
hello1 list size is 13
hello3 list size is 14
hello2 list size is 14
hello1 list size is 15
hello3 list size is 17
hello2 list size is 17

----以上结果每个线程执行6次list添加,结果list长度只有17个,数据丢失,线程不安全。

2)线程安全案例:

public class VectorThread implements Runnable {  
    String name;  
    Vector v;  
  
    VectorThread(String name, Vector v) {  
        this.name = name;  
        this.v = v;  
    }  
  
    public void run() {  
        System.out.println(name + "start");  
        for(int i = 0; i<6; i++){
            v.add(name + ".add");  
            System.out.println(name + " vector size is " + v.size());  
  
            try {  
                Thread.sleep(10);  
            } catch(InterruptedException e) {  
                System.out.println(e.getMessage());  
            }  
        }  
    }  
  
    public static void main(String args[]) throws InterruptedException {  
  
        Vector v = new Vector();  
  
        VectorThread hello1 = new VectorThread("hello1", v);  
        VectorThread hello2 = new VectorThread("hello2", v);  
        VectorThread hello3 = new VectorThread("hello3", v);  
  
        Thread h1 = new Thread(hello1);  
        Thread h2 = new Thread(hello2);  
        Thread h3 = new Thread(hello3);  
        h1.start();  
        h2.start();  
        h3.start();  
    }  
}  

执行结果:

hello2start
hello2 vector size is 1
hello3start
hello1start
hello1 vector size is 3
hello3 vector size is 2
hello1 vector size is 5
hello3 vector size is 5
hello2 vector size is 6
hello1 vector size is 7
hello2 vector size is 8
hello3 vector size is 9
hello3 vector size is 12
hello2 vector size is 11
hello1 vector size is 11
hello3 vector size is 14
hello2 vector size is 15
hello1 vector size is 14
hello3 vector size is 17
hello2 vector size is 17
hello1 vector size is 18

--------------线程安全,结果执行无论多少次,最后vector的长度都是18,数据添加完整安全,但是长度在线程抢占时还是有size相同的情况,需要在外围另外添加synchronized

3)外围加锁后的线程安全案例

public class VectorThread2 implements Runnable {  
    String name;  
    Vector v;  
  
    VectorThread2(String name, Vector v) {  
        this.name = name;  
        this.v = v;  
    }  
  
    public void run() {  
        System.out.println(name + "start");  
        
        for(int i = 0; i<6; i++){
        	synchronized(v){
        		v.add(name + ".add");  
        		System.out.println(name + " vector size is " + v.size());  
        	}
            try {  
                Thread.sleep(10);  
            } catch(InterruptedException e) {  
                System.out.println(e.getMessage());  
            }  
        }  
    }  
  
    public static void main(String args[]) throws InterruptedException {  
  
        Vector v = new Vector();  
  
        VectorThread2 hello1 = new VectorThread2("hello1", v);  
        VectorThread2 hello2 = new VectorThread2("hello2", v);  
        VectorThread2 hello3 = new VectorThread2("hello3", v);  
  
        Thread h1 = new Thread(hello1);  
        Thread h2 = new Thread(hello2);  
        Thread h3 = new Thread(hello3);  
        h1.start();  
        h2.start();  
        h3.start();  
    }  
}  

执行结果:

hello2start
hello2 vector size is 1
hello2 vector size is 2
hello2 vector size is 3
hello2 vector size is 4
hello1start
hello1 vector size is 5
hello2 vector size is 6
hello1 vector size is 7
hello2 vector size is 8
hello1 vector size is 9
hello3start
hello3 vector size is 10
hello1 vector size is 11
hello3 vector size is 12
hello1 vector size is 13
hello3 vector size is 14
hello1 vector size is 15
hello3 vector size is 16
hello3 vector size is 17
hello3 vector size is 18

----------------------这样无论执行多少次既不会线程抢占,也不会数据丢失

4)使用synchronizedXxx封装后集合线程安全案例(效果和Vector类似)

public class SynchronizedListRight implements Runnable {  
    String name;  
    List list;
  
    SynchronizedListRight(String name, List list) {  
        this.name = name;  
        this.list = list;  
    }  
  
    public void run() {  
        System.out.println(name + "start");  
        
        for(int i = 0; i<6; i++){
        	synchronized(list){
        		list.add(name + ".add");  
        		System.out.println(name + " list size is " + list.size());  
        	}
            try {  
                Thread.sleep(10);  
            } catch(InterruptedException e) {  
                System.out.println(e.getMessage());  
            }  
        }  
    }  
  
    public static void main(String args[]) throws InterruptedException {  
  
    	List list = Collections.synchronizedList(new ArrayList());
  
        SynchronizedListRight hello1 = new SynchronizedListRight("hello1", list);  
        SynchronizedListRight hello2 = new SynchronizedListRight("hello2", list);  
        SynchronizedListRight hello3 = new SynchronizedListRight("hello3", list);  

        Thread h1 = new Thread(hello1);  
        Thread h2 = new Thread(hello2);  
        Thread h3 = new Thread(hello3);  
        h1.start();  
        h2.start();  
        h3.start();  
    }  
}  

执行结果:

hello3start
hello1start
hello3 list size is 1
hello2start
hello1 list size is 2
hello2 list size is 3
hello1 list size is 4
hello3 list size is 5
hello2 list size is 6
hello1 list size is 7
hello3 list size is 8
hello2 list size is 9
hello2 list size is 10
hello3 list size is 11
hello1 list size is 12
hello2 list size is 13
hello3 list size is 14
hello1 list size is 15
hello1 list size is 16
hello2 list size is 17
hello3 list size is 18

----------效果和第三种案例一样

转载于:https://my.oschina.net/u/2329948/blog/785846

你可能感兴趣的:(java)