java set并发_并发Set与Map的使用

import java.util.Set;

import java.util.Map.Entry;

import java.util.concurrent.ConcurrentHashMap;

import java.util.concurrent.ConcurrentSkipListSet;

public class TestMap {

//public static HashMap m=new HashMap();

public static ConcurrentHashMap m=new ConcurrentHashMap();

public static ConcurrentSkipListSet s=new ConcurrentSkipListSet();

public static void main(String[] args){

testMap();

//testSet();

}

public static void testSet(){

new Thread(){

public void run(){

for(int i=0;i<100;i++){

s.add(i);

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}.start();

new Thread(){

public void run(){

while(true){

System.out.println("cur:"+s.size());

for(Integer entry:s){

System.out.println(entry);

s.remove(entry);

}

try {

Thread.sleep(200);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//entrys.clear(); 错误

//m.clear(); 错误

}

}

}.start();

}

public static void testMap(){

new Thread(){

public void run(){

for(int i=0;i<100;i++){

m.put(i , i);

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}.start();

new Thread(){

public void run(){

while(true){

Set entrys=m.entrySet();

System.out.println("cur:"+entrys.size());

for(Entry entry:entrys){

System.out.println(entry.getKey()+":"+entry.getValue());

m.remove(entry.getKey());

}

try {

Thread.sleep(200);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//entrys.clear();

//m.clear();

}

}

}.start();

}

}

代码如上,如果多线程操作使用HashMap 或HashSet,一个线程写,一个线程读则会产生ConcurrentModificationException。

需要注意的是,运行结果你会发现使用并发ConcurrentHashMap与ConcurrentSkipListSet时,当前遍历的Set并不是size总是一样的,也就是如果另一个线程增加,size会动态增加,遍历次数会动态增加 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值