Java 集合线程安全

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

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

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

1)线程不安全案例:

1public class ListThread implements Runnable { 2 String name; 3 List<String> l; 4 5 ListThread(String name, List<String> l) { 6 this.name = name; 7 this.l = l; 8 } 9 10 public void run() { 11 System.out.println(name + "start"); 12 for(int i = 0; i<6; i++){ 13 l.add(name + ".add"); 14 System.out.println(name + " list size is " + l.size()); 15 try { 16 Thread.sleep(10); 17 } catch(InterruptedException e) { 18 System.out.println(e.getMessage()); 19 } 20 } 21 } 22 23 public static void main(String args[]) throws InterruptedException { 24 25 List<String> l = new ArrayList<String>(); 26 27 ListThread hello1 = new ListThread("hello1", l); 28 ListThread hello2 = new ListThread("hello2", l); 29 ListThread hello3 = new ListThread("hello3", l); 30 31 Thread h1 = new Thread(hello1); 32 Thread h2 = new Thread(hello2); 33 Thread h3 = new Thread(hello3); 34 h1.start(); 35 h2.start(); 36 h3.start(); 37 } 38}

执行结果:

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)线程安全案例:

1public class VectorThread implements Runnable { 2 String name; 3 Vector<String> v; 4 5 VectorThread(String name, Vector<String> v) { 6 this.name = name; 7 this.v = v; 8 } 9 10 public void run() { 11 System.out.println(name + "start"); 12 for(int i = 0; i<6; i++){ 13 v.add(name + ".add"); 14 System.out.println(name + " vector size is " + v.size()); 15 16 try { 17 Thread.sleep(10); 18 } catch(InterruptedException e) { 19 System.out.println(e.getMessage()); 20 } 21 } 22 } 23 24 public static void main(String args[]) throws InterruptedException { 25 26 Vector<String> v = new Vector<String>(); 27 28 VectorThread hello1 = new VectorThread("hello1", v); 29 VectorThread hello2 = new VectorThread("hello2", v); 30 VectorThread hello3 = new VectorThread("hello3", v); 31 32 Thread h1 = new Thread(hello1); 33 Thread h2 = new Thread(hello2); 34 Thread h3 = new Thread(hello3); 35 h1.start(); 36 h2.start(); 37 h3.start(); 38 } 39}

执行结果:

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)外围加锁后的线程安全案例

1public class VectorThread2 implements Runnable { 2 String name; 3 Vector<String> v; 4 5 VectorThread2(String name, Vector<String> v) { 6 this.name = name; 7 this.v = v; 8 } 9 10 public void run() { 11 System.out.println(name + "start"); 12 13 for(int i = 0; i<6; i++){ 14 synchronized(v){ 15 v.add(name + ".add"); 16 System.out.println(name + " vector size is " + v.size()); 17 } 18 try { 19 Thread.sleep(10); 20 } catch(InterruptedException e) { 21 System.out.println(e.getMessage()); 22 } 23 } 24 } 25 26 public static void main(String args[]) throws InterruptedException { 27 28 Vector<String> v = new Vector<String>(); 29 30 VectorThread2 hello1 = new VectorThread2("hello1", v); 31 VectorThread2 hello2 = new VectorThread2("hello2", v); 32 VectorThread2 hello3 = new VectorThread2("hello3", v); 33 34 Thread h1 = new Thread(hello1); 35 Thread h2 = new Thread(hello2); 36 Thread h3 = new Thread(hello3); 37 h1.start(); 38 h2.start(); 39 h3.start(); 40 } 41}

执行结果:

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类似)

1public class SynchronizedListRight implements Runnable { 2 String name; 3 List<String> list; 4 5 SynchronizedListRight(String name, List<String> list) { 6 this.name = name; 7 this.list = list; 8 } 9 10 public void run() { 11 System.out.println(name + "start"); 12 13 for(int i = 0; i<6; i++){ 14 synchronized(list){ 15 list.add(name + ".add"); 16 System.out.println(name + " list size is " + list.size()); 17 } 18 try { 19 Thread.sleep(10); 20 } catch(InterruptedException e) { 21 System.out.println(e.getMessage()); 22 } 23 } 24 } 25 26 public static void main(String args[]) throws InterruptedException { 27 28 List<String> list = Collections.synchronizedList(new ArrayList<String>()); 29 30 SynchronizedListRight hello1 = new SynchronizedListRight("hello1", list); 31 SynchronizedListRight hello2 = new SynchronizedListRight("hello2", list); 32 SynchronizedListRight hello3 = new SynchronizedListRight("hello3", list); 33 34 Thread h1 = new Thread(hello1); 35 Thread h2 = new Thread(hello2); 36 Thread h3 = new Thread(hello3); 37 h1.start(); 38 h2.start(); 39 h3.start(); 40 } 41}

执行结果:

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

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

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Java 集合线程安全 - HelloWorld