Java 集合线程安全
2016-11-10 00:00
190 查看
摘要: 给出四种案例
1.内外线程不安全list案例;
2.内线程安全Vecctor案例;
3.内外线程安全Vector案例;
4.使用synchronizedXxx封装后集合内外线程安全案例;
线程不安全的的集合有(HashSet,TreeSet,ArrayList,ArrayDeque,LinkedList,HashMap,TreeMap);
线程安全的集合有(Vector,HashTable);
Java给你的保证的线程安全,是说它的方法是执行是排它的,而不是对这个对象本身的多次调用情况下,还是安全的。
1)线程不安全案例:
执行结果:
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)线程安全案例:
执行结果:
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)外围加锁后的线程安全案例
执行结果:
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类似)
执行结果:
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
----------效果和第三种案例一样
1.内外线程不安全list案例;
2.内线程安全Vecctor案例;
3.内外线程安全Vector案例;
4.使用synchronizedXxx封装后集合内外线程安全案例;
线程不安全的的集合有(HashSet,TreeSet,ArrayList,ArrayDeque,LinkedList,HashMap,TreeMap);
线程安全的集合有(Vector,HashTable);
Java给你的保证的线程安全,是说它的方法是执行是排它的,而不是对这个对象本身的多次调用情况下,还是安全的。
1)线程不安全案例:
public class ListThread implements Runnable { String name; List<String> l; ListThread(String name, List<String> 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<String> l = new ArrayList<String>(); 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<String> v; VectorThread(String name, Vector<String> 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<String> v = new Vector<String>(); 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<String> v; VectorThread2(String name, Vector<String> 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<String> v = new Vector<String>(); 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<String> list; SynchronizedListRight(String name, List<String> 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<String> list = Collections.synchronizedList(new ArrayList<String>()); 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
----------效果和第三种案例一样
相关文章推荐
- java 集合中线程安全问题
- Java集合中那些类是线程安全的
- Java多线程:线程安全和非线程安全的集合对象
- java各种集合的线程安全
- 线程安全的集合类(java并发编程第5章)
- Java 集合 线程安全
- Java集合中那些类是线程安全的 以及 Map线程安全几种实现方法
- Java知识点整理:第十章:包装类、集合的复制、集合线程同步化(线程安全)、集合与数组的相互转换
- Java集合中那些类是线程安全的
- Java多线程理解:线程安全的集合对象
- Java 集合 构建线程安全
- Java集合中那些类是线程安全的
- Java集合中那些类是线程安全的
- Java集合中那些类是线程安全的
- java中的常用集合和线程安全
- Java中线程安全集合的使用小结
- java中线程安全的集合对象
- java.util.concurrent包中线程安全的集合简介
- Java集合中那些类是线程安全的
- java各种集合的线程安全