ConcurrentHashMap的使用和理解

17 篇文章 0 订阅

ConcurrentHashMap实现 数据在多线程之间的可见性

 //这种类似于volatile 可以实现多线程操作数据时数据在多个线程之间的可见性
 static Map<Long, String> conMap = new ConcurrentHashMap<Long, String>();

        public static void main (String[]args) throws InterruptedException {
            for (long i = 0; i < 5; i++) {
                conMap.put(i, i + "");
            }

            Thread thread = new Thread(new Runnable() {
                public void run() {
                    conMap.put(100l, "100");
                    //Log.d("ADD:" + 100);
                    System.out.println("ADD:" + 100);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            });
            Thread thread2 = new Thread(new Runnable() {
                public void run() {
                    for (Iterator<Map.Entry<Long, String>> iterator = conMap.entrySet().iterator(); iterator.hasNext(); ) {
                        Map.Entry<Long, String> entry = iterator.next();
                        //Log.d(entry.getKey() + " - " + entry.getValue());
                        System.out.println(entry.getKey() + " - " + entry.getValue());
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            thread.start();
            thread2.start();

            Thread.sleep(3000);
            //Log.d("--------");
            System.out.println("===============");
            for (Map.Entry<Long, String> entry : conMap.entrySet()) {
                //Log.d(entry.getKey() + " " + entry.getValue());
                System.out.println(entry.getKey() + " " + entry.getValue());
            }

        }

输出:

    //ADD:100
    //0 - 0
    //1 - 1
    //2 - 2
    //3 - 3
    //4 - 4
    //100 - 100
    //===============
    //0 0
    //1 1
    //2 2
    //3 3
    //4 4
    //100 100

对比Volatile

static volatile boolean notice = false;

    public static void main(String[] args) {

        List<String> list = new ArrayList<>();
        // 实现线程A
        Thread threadA = new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                list.add("abc");
                System.out.println("线程A" + Thread.currentThread().getName() + "向list中添加一个元素,此时list中的元素个数为:" + list.size());
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (list.size() == 5) {
                    notice = true;
                }
            }
        });
        // 实现线程B
        Thread threadB = new Thread(() -> {
            while (true) {
                if (notice) {
                    System.out.println("线程B" + Thread.currentThread().getName() + "收到通知,开始执行自己的业务...");
                    break;
                }
            }
        });
        // 需要先启动线程B
        threadB.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 再启动线程A
        threadA.start();
    }

输出:

线程AThread-0向list中添加一个元素,此时list中的元素个数为:1
线程AThread-0向list中添加一个元素,此时list中的元素个数为:2
线程AThread-0向list中添加一个元素,此时list中的元素个数为:3
线程AThread-0向list中添加一个元素,此时list中的元素个数为:4
线程AThread-0向list中添加一个元素,此时list中的元素个数为:5
线程AThread-0向list中添加一个元素,此时list中的元素个数为:6
线程BThread-1收到通知,开始执行自己的业务...
线程AThread-0向list中添加一个元素,此时list中的元素个数为:7
线程AThread-0向list中添加一个元素,此时list中的元素个数为:8
线程AThread-0向list中添加一个元素,此时list中的元素个数为:9
线程AThread-0向list中添加一个元素,此时list中的元素个数为:10

如果不用Volatile则输出

线程AThread-0向list中添加一个元素,此时list中的元素个数为:1
线程AThread-0向list中添加一个元素,此时list中的元素个数为:2
线程AThread-0向list中添加一个元素,此时list中的元素个数为:3
线程AThread-0向list中添加一个元素,此时list中的元素个数为:4
线程AThread-0向list中添加一个元素,此时list中的元素个数为:5
线程AThread-0向list中添加一个元素,此时list中的元素个数为:6
线程AThread-0向list中添加一个元素,此时list中的元素个数为:7
线程AThread-0向list中添加一个元素,此时list中的元素个数为:8
线程AThread-0向list中添加一个元素,此时list中的元素个数为:9
线程AThread-0向list中添加一个元素,此时list中的元素个数为:10

由于单纯线程之间执行是互不干扰的,所以AThread-0元素中元素数量为5时,BThread-1也不知道,即不知道notice是否为true,所以不会输出!!!

      所以实现数据在多线程之间的可见性可以用ConcurrentHashMap装数据或者使用Volatile

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值