java.util.Concurrent.CopyOnWriteArrayList<E>分析

与ArrayList比较

1、 ArrayList每次扩容和删除都需要把原数组复制到新数组中,开销很大
2、 CopyOnWriteArrayList是ArrayList 的一个线程安全的变体

读写分离

CopyOnWriteArrayList:

  • 写操作在一个复制的数组上进行,读操作在原数组中进行,读写分离互不影响;
  • 写操作需要加锁,防止并发写入时导致写入数据丢失;
  • 写操作结束后需要把原数组指向新的复制数组。
    add()方法:
public boolean add(E e) {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            Object[] elements = getArray();
            int len = elements.length;
            Object[] newElements = Arrays.copyOf(elements, len + 1);
            newElements[len] = e;
            setArray(newElements);
            return true;
        } finally {
            lock.unlock();
        }
    }
    
    @SuppressWarnings("unchecked")
    private E get(Object[] a, int index) {
        return (E) a[index];
    }
适用场景

CopyOnWriteArrayList 在写操作的同时允许读操作,大大提高了读操作的性能,因此很适合读多写少的应用场景。

缺点

CopyOnWriteArrayList 有其缺陷:

  • 内存占用:在写操作时需要复制一个新的数组,使得内存占用为原来的两倍左右;
  • 数据不一致:读操作不能读取实时性的数据,因为部分写操作的数据还未同步到读数组中。

所以 CopyOnWriteArrayList 不适合内存敏感以及对实时性要求很高的场景。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<properties> <hudson.security.AuthorizationMatrixProperty> <inheritanceStrategy class="org.jenkinsci.plugins.matrixauth.inheritance.InheritParentStrategy"/> <permission>USER:hudson.model.Item.Read:anonymous</permission> </hudson.security.AuthorizationMatrixProperty> <jenkins.model.BuildDiscarderProperty> <strategy class="hudson.tasks.LogRotator"> <daysToKeep>90</daysToKeep> <numToKeep>-1</numToKeep> <artifactDaysToKeep>30</artifactDaysToKeep> <artifactNumToKeep>-1</artifactNumToKeep> </strategy> </jenkins.model.BuildDiscarderProperty> <com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin="gitlab-plugin@1.7.7"> <gitLabConnection>src.ift.run</gitLabConnection> <jobCredentialId/> <useAlternativeCredential>false</useAlternativeCredential> </com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty> <com.sonyericsson.rebuild.RebuildSettings plugin="rebuild@1.34"> <autoRebuild>false</autoRebuild> <rebuildDisabled>false</rebuildDisabled> </com.sonyericsson.rebuild.RebuildSettings> <hudson.plugins.throttleconcurrents.ThrottleJobProperty plugin="throttle-concurrents@2.11"> <maxConcurrentPerNode>3</maxConcurrentPerNode> <maxConcurrentTotal>0</maxConcurrentTotal> <categories class="java.util.concurrent.CopyOnWriteArrayList"/> <throttleEnabled>true</throttleEnabled> <throttleOption>project</throttleOption> <limitOneJobWithMatchingParams>false</limitOneJobWithMatchingParams> <paramsToUseForLimit/> <configVersion>1</configVersion> </hudson.plugins.throttleconcurrents.ThrottleJobProperty> <jenkins.branch.RateLimitBranchProperty_-JobPropertyImpl plugin="branch-api@2.1071.v1a_188a_562481"> <durationName>hour</durationName> <count>40</count> <userBoost>true</userBoost> </jenkins.branch.RateLimitBranchProperty_-JobPropertyImpl> </properties> 请解释这一段jenkins配置
07-14

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值