【Java基础】ConcurrentHashMap原理分析及应用

Java ConcurrentHashMap 的并发统计功能实现 在多线程编程中,我们经常需要对数据进行统计操作。普通的 HashMap 不是线程安全的,在多线程环境下使用会出现数据不一致的问题。而 ConcurrentHashMap 是 Java 提供的线程安全的哈希表,它可以在多线程环境下高效地进行并发操作。本文的目的就是详细介绍如何利用 ConcurrentHashMap 实现并发统计功能,范围涵盖其核心概念、原理、代码实现以及实际应用等方面。 阅读详情

引言

本篇博客是基于之前的HashMap开展的,读者若是对HashMap不甚了解的话,可以参看之前的博客。
【Java基础】HashMap底层实现原理 附源码分析及面试题,让你深入Java集合
简单来说,ConcurrentHashMap是HashMap的并发版本,适合在并发环境下使用。
另外,本文关于ConcurrentHashMap的源码是基于JDK1.8的。

原理分析

ConcurrentHashMap原理:JDK1.7中,ConcurrentHashMap 采用了分段锁技术,其中 Segment是ConcurrentHashMap的内部类, 继承于 ReentrantLock。不会像 Hashtable 那样不管是 put 还是 get 操作都需要做同步处理,理论上 ConcurrentHashMap 支持 CurrencyLevel (Segment 数组数量)的线程并发。每当一个线程占用锁访问一个 Segment 时,不会影响到其他的 Segment。如下图所示:

在这里插入图片描述
但是从JDK1.8开始,ConcurrentHashMap就抛弃了原有的分段锁,而是改用CAS+synchronized来保证并发安全性。同时在JDK1.8中,也弃用了HashEntry,改用了Node(但功能相同):

static class Node<K,V> implements Map.Entry<K,V> {
        final int hash;
        final K key;
        volatile V val;
        volatile Node<K,V> next;

        Node(int hash, K key, V val, Node<K,V> next) {
            this.hash = hash;
            this.key = key;
            this.val = val;
            this.next = next;
        }

        public final K getKey()       { return key; }
        public final V getValue()     { return val; }
        public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
        public final String toString(){ return key + "=" + val; }
        public final V setValue(V value) {
            throw new UnsupportedOperationException();
        }
        ……
  }

不难发现,val 和next 都用了 volatile 修饰,保证了并发条件下的可见性。

put方法

先上源码:

final V putVal(K key, V value, boolean onlyIfAbsent) {
        if (key == null || value == null) throw new NullPointerException();
        int hash = spread(key.hashCode());
        int binCount = 0;
        /* 注释1 */
        for (Node<K,V>[] tab = table;;) {
            Node<K,V> f; int n, i, fh;
            /* 注释2 */
            if (tab == null || (n = tab.length) == 0)
                tab = initTable();
                /* 注释3 */
            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
                if (casTabAt(tab, i, null,
                             new Node<K,V>(hash, key, value, null)))
                    break;                   // no lock when adding to empty bin
            }
            /* 注释4 */
            else if ((fh = f.hash) == MOVED)
                tab = helpTransfer(tab, f);
            else {
                V oldVal = null;
                /* 注释5 */
                synchronized (f) {
                    if (tabAt(tab, i) == f) {
                        if (fh >= 0) {
                            binCount = 1;
                            for (Node<K,V> e = f;; ++binCount) {
                                K ek;
                                if (e.hash == hash &&
                                    ((ek = e.key) == key ||
                                     (ek != null && key.equals(ek)))) {
                                    oldVal = e.val;
                                    if (!onlyIfAbsent)
                                        e.val = value;
                                    break;
                                }
                                Node<K,V> pred = e;
                                if ((e = e.next) == null) {
                                    pred.next = new Node<K,V>(hash, key,
                                                              value, null);
                                    break;
                                }
                            }
                        }
                        else if (f instanceof TreeBin) {
                            Node<K,V> p;
                            binCount = 2;
                            if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
                                                           value)) != null) {
                                oldVal = p.val;
                                if (!onlyIfAbsent)
                                    p.val = value;
                            }
                        }
                    }
                }
                if (binCount != 0) {
                /* 注释6 */
                    if (binCount >= TREEIFY_THRESHOLD)
                        treeifyBin(tab, i);
                    if (oldVal != null)
                        return oldVal;
                    break;
                }
            }
        }
        addCount(1L, binCount);
        return null;
    }
  1. 根据 key 计算出 hashCode ;
  2. 判断是否需要进行初始化;
  3. f 即为当前 key 定位出的 Node,如果为空表示当前位置可以写入数据,利用 CAS 尝试写入,失败则自旋保证成功;
  4. 如果当前位置的 hashCode == MOVED (值为-1),则需要进行扩容;
  5. 如果都不满足,则利用 synchronized 锁写入数据;
  6. 如果数量大于 TREEIFY_THRESHOLD (值为8)则要转换为红黑树。

get方法

public V get(Object key) {
        Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
        int h = spread(key.hashCode());
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (e = tabAt(tab, (n - 1) & h)) != null) {
            if ((eh = e.hash) == h) {
                if ((ek = e.key) == key || (ek != null && key.equals(ek)))
                    return e.val;
            }
            else if (eh < 0)
                return (p = e.find(h, key)) != null ? p.val : null;
            while ((e = e.next) != null) {
                if (e.hash == h &&
                    ((ek = e.key) == key || (ek != null && key.equals(ek))))
                    return e.val;
            }
        }
        return null;
    }

根据计算出来的 hashCode 寻址,如果就在桶上那么直接返回值,如果是红黑树那就按照树的方式获取值,否则就按照链表的方式遍历获取值。

参考阅读

Java并发包concurrent—ConcurrentHashMap
自旋锁与互斥锁的对比、手工实现自旋锁

Keil C51开发必看:详解data/xdata/bdata内存分配陷阱与避坑指南 本文深入解析Keil C51开发中的data/xdata/bdata内存分配陷阱,提供针对51芯片的高效避坑指南。通过真实案例揭示Keil编译器的隐藏规则,分享内存优化策略和调试技巧,帮助开发者解决data溢出、xdata访问异常等典型问题,提升嵌入式开发效率。 阅读详情

相关推荐

深入浅出卡尔曼滤波器算法

深入浅出讲解卡尔曼滤波器的工作原理及其公式推导,讨论卡尔曼滤波器的核心公式所代表的意义及个人的理解。

m0_37835056的博客 1368

Java集合---ConcurrentHashMap原理分析

一、背景: 线程不安全的HashMap     因为多线程环境下,使用Hashmap进行put操作会引起死循环,导致CPU利用率接近100%,所以在并发情况下不能使用HashMap。   效率低下的HashTable容器      HashTable容器使用synchronized来保证线程安全,但在线程竞争激烈的情况下HashTable的效率非常低下。因为当一个线程访问HashTabl...

weixin_34128237的博客 680

毫米波雷达入门知识

毫米波雷达入门知识

ssbzxcvbnm1234567的博客 5460

ConcurrentHashMap源码分析(黑马面试题笔记)

了解ConcurrentHashMap容器的初始化

Guo_Liwei的博客 1220

NDIAurora 6D、5D传感器裸线焊接与烧录 6D传感器型号:10001742;5D传感器型号:610157

NDI Aurora 6D、5D传感器裸线焊接与烧录

yanghong2000719的博客 1212

模仿学习算法——ATC / Diffusion Policy

最近又开始接触具身智能相关的一些工作,本文主要介绍三篇较为基础的用来做模仿学习的算法,后续有空再进一步学习分享RT系列和Pi0等文章ACT算法全称为Action Chunking with Transformers,发表于《Learning Fine-Grained Bimanual Manipulation with Low-Cost Hardware》,该论文主要内容为如何使用低成本的硬件和模仿学习算法来实的双手操作,我们主要对算法部分进行剖析ACT算法的网络结构如下图所示,使用的采用的是生成式模型VA

weixin_44580210的博客 5277

ConcurrentHashMap原理分析

一、出现背景      1、线程不安全的HashMap      因为多线程环境下,使用Hashmap进行put操作会引起死循环,导致CPU利用率接近100%,所以在并发情况下不能使用HashMap。 2、效率低下的HashTable容器      HashTable容器使用synchronized来保证线程安全,但在线程竞争激烈的情况下HashTable的效率非常低下。因为当一个线程访问...

weixin_33910385的博客 439

YOLOv11的流形学习压缩:局部线性嵌入(LLE)引导的特征降维,实现在CSP模块中嵌入流形结构保持约束

摘要 本文探讨了流形学习在YOLOv11目标检测模型中的应用,重点研究局部线性嵌入(LLE)方法对CSP模块的特征压缩优化。针对深度学习模型的高维特征冗余问题,LLE通过三个关键步骤实现非线性降维:寻找k近邻、计算重构权重和低维嵌入,相比传统PCA方法能更好地保持数据的局部结构。文章详细分析了LLE的数学原理和参数选择策略,并提出在YOLOv11的CSP模块中嵌入流形约束的创新设计。通过特征分割前、处理过程和融合阶段引入流形一致性损失函数,该方法能在降低特征维度的同时保持原始流形结构,从而提升模型的计算效率

本博客聚焦 YOLOv11 全流程落地,涵盖架构优化、数据集处理、训练技巧与多场景部署,兼及国产数据库、Java 开发与 AI 模型应用,内容兼顾理论与工程实践,为开发者提供系统干货,助力高效提升技术能力。 1390

ConcurrentHashMap原理深度解析

在Java中,ConcurrentHashMap(简称CHM)是一个线程安全的哈希表实现,它支持高并发场景下的读写操作。与HashMap和Hashtable不同,ConcurrentHashMap在设计和实现上做了大量的优化,以便于在保证线程安全的前提下提供更高的吞吐量和更低的延迟。本文将详细解析ConcurrentHashMap的原理、线程安全性的实现方式、扩容机制以及为什么选择这样的扩容策略,并结合源代码进行说明。本文基于jdk1.8的源码作为示例。

Jz_Stu的博客 2920

基于深度学习的商品标签识别系统:UI界面 + YOLOv8/v7/v6/v5代码 + 训练数据集

基于YOLO系列模型的商品标签识别系统能够有效解决商品信息提取的自动化问题。通过合理的数据集准备、模型训练和UI设计,我们可以实现一个功能强大的商品标签识别系统。希望本文提供的内容能够为大家提供有价值的帮助。

YOLO 477

19. OTA功能 - 1

耳机采用AB分区的OTA方式,AB分区详见分区表,通过手机蓝牙下发更新文件。

雪飞_海 653

ConcurrentHashMap的原理分析

JDK1.7: 底层数据结构:数组(sgement)、数组(HashEntry)、链表(HashEntry节点) 两个主要的内部类: class Segment内部类,继承ReentrantLock,有一个HashEntry数组,用来存储链表头结点 class HashEntry 定义的节点,里面存储的数据和下一个节点 主要方法: get()方法: 1、第一次哈希 找到 对应的Segment段,调用Segment中的get方法 2、再次哈希找到对应的链表, 3、最后在链表中查找。 put()方法: 1、首

致力于不留技术债务cuizb.top 974

MATLAB代码实现了一个柱塞泵配流盘过流面积的计算和可视化功能

该MATLAB代码实现了一个柱塞泵配流盘过流面积的计算和可视化功能。输出曲线可直接用于验证设计的合理性(如是否存在过流面积突变或不足)。的不同范围,选择对应的几何模型计算过流面积。通过分段逻辑计算柱塞腔在不同旋转角度(过流面积在特定条件下会被限制(如。),确保结果符合物理实际。条件分支,根据旋转角度。

huanghm88的专栏 590

Java7-8下的HashMap与ConcurrentHashMap

本文章来源于:https://github.com/Zeb-D/my-review ,请star 强力支持,你的支持,就是我的动力。 [TOC] Java7/8 下 HashMap 与 ConcurrentHashMap 网上关于 HashMap 和 ConcurrentHashMap 的文章确实不少,不过有很多没那么全面,自己也写一篇,把细节说清楚说透,尤其像 Java8 中的 Concurr...

在此立一个IT职业上的flag:https://github.com/Zeb-D/my-review 1万+

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

阿里十年架构师,教你深度分析ConcurrentHashMap原理分析

ConcurrentHashMap的初步使用及场景 CHM的使用 ConcurrentHashMap是J.U.C包里面提供的一个线程安全并且高效的HashMap,所以ConcurrentHashMap在并发编程的场景中使用的频率比较高,那么这一节课我们就从ConcurrentHashMap的使用上以及源码层面来分析ConcurrentHashMap到底是如何实现安全性的 ap...

qq_39662660的博客 973

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

ConcurrentHashMap内部原理分析

集合是编程中最常用的数据结构。而谈到并发,几乎总是离不开集合这类高级数据结构的支持。比如两个线程需要同时访问一个中间临界区(Queue),比如常会用缓存作为外部文件的副本(HashMap)。这篇文章主要分析jdk1.5的3种并发集合类型(concurrent,copyonright,queue)中的ConcurrentHashMap,让我们从原理上细致的了解它们,能够让我们在深度项目开发中获益非浅

iTuring_Zhou的专栏 526

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

java ConcurrentHashMap原理分析

空间阿斯顿和发卡

u012760183的专栏 540

HashMap、ConcurrentHashMap原理分析

集合(Collection)是编程中常用的数据结构,而并发也是服务器端编程常用的技术之一,并发总是离不开集合这类高级数据结构的支持。比如两个线程需要同时访问一个中间临界区(Queue),比如常会用缓存作为外部文件的副本(HashMap)。而Map这种以键值对为元素的数据结构也是集合中最常用到的。Map家族中的三大类:HashMap、HashTable、ConcurrentHashMap。前者非线程...

weixin_34292287的博客 214

三十九、并发编程之ConcurrentHashMap源码解析

一、ConcurrentHashMap实现原理 众所周知,哈希表是中非常高效,复杂度为O(1)的数据结构,在Java开发中,我们最常见到最频繁使用的就是HashMap和HashTable,但是在线程竞争激烈的并发场景中使用都不够合理。 1.HashMap HashMap是线程不安全的,在并发环境下,可能会形成环状链表(扩容时可能造成),导致get操作时,cpu空转,所以,在并发环境中使用HashM...

白夜行悟空 390

hashMap & linkedHashMap & concurrentHashMap 原理----legend050709

(1)hashMap (2)linkedHashMap ---------------- (1)hashMap (1.1)hash Hash 就是把任意长度的输入(又叫做预映射, pre-image),通过哈希算法,变换成固定长度的输出(通常是整型),该输出就是哈希值。这种转换是一种 压缩映射 ,也就是说,散列值的空间通常远小于输入的空间。不同的输入可能会散列成相同的输出,从而不可能...

legend050709的专栏 471

关于HashMap、Hashtable、ConcurrentHashMap等区别、ConcurrentHashMap实现线程安全的知识点总结

目录 一、ArrayList、LinkedList、Vector的区别 二、HashMap和Hashtable的区别 三、HashMap和ConcurrentHashMap的区别 四、HashMap和LinkedHashMap的区别 五、HashMap是非线程安全的原因及实现线程安全的方法 六、ConcurrentHashMap实现线程安全的原理 一、ArrayList、Linked...

Xx丶浅时光的博客 850

Java编程中的并发容器ConcurrentHashMap设计与应用详解

随着Java版本的演进,尤其是到了Java 8,ConcurrentHashMap的内部实现发生了重大变化,放弃了分段锁,转而采用更为优化的CAS(Compare-And-Swap)操作、synchronized锁以及更精细化的锁粒度(锁住单个桶的头节点),并引入了红黑树来解决哈希冲突严重时的性能问题,使其在并发性能和空间效率上达到了新的高度。:这是一个非常强大的方法。在Java 8的实现中,get操作通常是无锁的,因为它读取的是volatile类型的value,保证了可见性,这使得读操作的性能非常高。

RussellFF的博客 892

并发容器(Map、List、Set)实战及其原理分析

Java的集合容器框架中,主要有四大类别:List、Set、Queue、Map,大家熟知的这些集合类ArrayList、LinkedList、HashMap这些容器都是非线程安全的。所以,Java先提供了同步容器供用户使用。同步容器可以简单地理解为通过synchronized来实现同步的容器,比如Vector、Hashtable以及SynchronizedList等容器。这样做的代价是削弱了并发性,当多个线程共同竞争容器级的锁时,吞吐量就会降低。因此为了解决同步容器的性能问题,所以才有了并发容器。

weixin_43164559的博客 1301

深入剖析 ConcurrentHashMap:Java 并发编程的基石

ConcurrentHashMap是Java中支持高并发的哈希表实现。Java7采用分段锁(Segment)机制,通过16个Segment实现并发控制,每个Segment内部维护链表结构。Java8进行了重大改进:引入红黑树优化查询性能,采用CAS操作减少锁竞争,并取消Segment直接使用Node数组。它提供put、get、computeIfAbsent等线程安全方法,适用于缓存系统、计数器等并发场景。随着Java版本迭代,ConcurrentHashMap不断优化以提高并发性能和稳定性,是多线程环境下高

2402_85030292的博客 961
上一篇: 【Session】关于在用户退出登录的时候清除相关session信息
下一篇: 【并发编程】基础系列(一):线程安全性
少侠露飞
博客等级 码龄9年 214粉丝 117原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址