Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
CoderViking
notes
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
notes
/
Java
/
concurrent
/
java-juc-CopyOnWriteArrayList.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
20 lines (16 loc) · 914 Bytes
master
Breadcrumbs
notes
/
Java
/
concurrent
/
java-juc-CopyOnWriteArrayList.java
Copy path
Top
File metadata and controls
Code
Blame
20 lines (16 loc) · 914 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
--------------------------
CopyOnWriteArrayList
|
--------------------------
#
适合
'安全的并发迭代'
操作的应用场景
*
添加操作多的时候
,
效率低
#
写入并复制
*
每次写入数据的时候
,
都会在低层复制出新的一个列表
,
再进行添加
#
它要处理的问题
*
Java的list在遍历时
,
若中途有别的线程对list容器进行修改
,
则会抛出
ConcurrentModificationException
异常
*
而
CopyOnWriteArrayList
由于其
"读写分离"
的思想
,
遍历和修改操作分别作用在不同的list容器
*
所以在使用迭代器进行遍历时候
,
也就不会抛出
ConcurrentModificationException
异常了
*
一句话
,
它就是为了解决在高并发下
,
执行修改不会抛出异常
#
它带来的问题
*
一是内存占用问题
,
毕竟每次执行写操作都要将原容器拷贝一份
,
数据量大时
,
对内存压力较大
,
可能会引起频繁GC
*
二是无法保证实时性
,
Vector对于读写操作均加锁同步
,
可以保证读和写的强一致性
*
而
CopyOnWriteArrayList
由于其实现策略的原因
,
写和读分别作用在新老不同容器上
,
在写操作执行过程中
,
读不会阻塞但读取到的却是老容器的数据
You can’t perform that action at this time.