Kotlin读写分离CopyOnWriteArrayList

Kotlin读写分离CopyOnWriteArrayList

基于读写分离思想Copy-On-Write(COW)设计的线程安全ArrayList变体,读读共享、写写互斥、读写互斥、写读互斥。读时直接读,不用加锁同步,线程安全。写/删/修改数据时复制一个副本,在新的List副本中写/删/改操作,写/删/改操作完成后,再把副本替换成原来的List。一般情况可当做普通ArrayList使用,规避List常见的ConcurrentModificationException异常问题。

优点:适合读多写少的场景,线程安全,读时候无须加锁(因为内部实现已经线程同步)。读写时候不会被阻塞。

缺点:(1)内存占用问题。占用内存高。因为在写操作时候,会复制一条相同的List,相当于每次写操作都会产生两倍内存占用需求。

(2)数据一致性问题。CopyOnWriteArrayList只能保证数据的最终一致性,不能保证实时一致性。

(3)数写速度问题。读时候快,写/删/改慢。

用普通ArrayList,多线程环境下抛错ConcurrentModificationException:

import java.util.Collections
import java.util.concurrent.Executors

fun main() {
    val nThreads = 200
    val mExecutorService = Executors.newFixedThreadPool(nThreads)

    var NUM = 0
    var list = Collections.synchronizedList(ArrayList<Int>())
    for (i in 1..nThreads) {
        mExecutorService.execute {
            while (true) {
                Thread.sleep(10)
                list.add(NUM++)

                println("@tid:${Thread.currentThread().threadId()}")
                list.forEachIndexed { index, j ->
                    //println("${list}")
                }
            }
        }
    }

    mExecutorService.shutdown()
}

改用CopyOnWriteArrayList,运行正常:

import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.Executors

fun main() {
    val nThreads = 200
    val mExecutorService = Executors.newFixedThreadPool(nThreads)

    var NUM = 0
    var list = CopyOnWriteArrayList<Int>()
    for (i in 1..nThreads) {
        mExecutorService.execute {
            while (true) {
                Thread.sleep(10)
                list.add(NUM++)

                println("@tid:${Thread.currentThread().threadId()}")
                list.forEachIndexed { index, j ->
                    //println("${list}")
                }
            }
        }
    }

    mExecutorService.shutdown()
}

避免了多线程环境读写ArrayList不安全的操作。

Java线程同步可重入锁ReentrantLock与Condition_zhangphil的博客-CSDN博客import java.util.LinkedList;import java.util.Queue;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;public class Main { private Queue...https://blog.csdn.net/zhangphil/article/details/92814997Java线程同步锁ReentrantLock和Condition中断等待程序前提条件满足_zhangphil的博客-CSDN博客import java.util.concurrent.TimeUnit;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;/** * ReentrantLock的lock和unlock必须成对使用且把需要同步的代码块包裹起来。 * lock-unlo...https://blog.csdn.net/zhangphil/article/details/92826986Java线程同步与阻塞ReentrantLock - Condition替换wait - notify_reentrantlock和wait notify_zhangphil的博客-CSDN博客import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;public class Main { private ReentrantLock lock = new ReentrantLock(false); private Condition con..._reentrantlock和wait notifyhttps://blog.csdn.net/zhangphil/article/details/96360104ReentrantLock替换synchronized解决多线程并发死锁,Java_reentrantlock解决死锁问题的代码_zhangphil的博客-CSDN博客Java并发多线程环境中,造成死锁的最简单的场景是:多线程中的一个线程T_A持有锁L1并且申请试图获得锁L2,而多线程中另外一个线程T_B持有锁L2并且试图申请获得锁L1。线程的锁申请操作是阻塞的,于是造成线程T_A和线程T_B无法正确获得想要的锁,两个线程被阻塞进入死锁状态。Java线程同步锁ReentrantLock和Condition中断等待程序前提条件满足_zhangphil的博客-CSDN博客。_reentrantlock解决死锁问题的代码https://blog.csdn.net/zhangphil/article/details/127548507Semaphore替换多线程synchronized解决并发环境死锁,Java_semaphore会照成死锁吗_zhangphil的博客-CSDN博客新Java线程Semaphore:并行环境下访问竞争资源控制Semaphore是从Java 1.5引入的Java线程新内容。Java并发多线程环境中,造成死锁的最简单的场景是:多线程中的一个线程T_A持有锁L1并且申请试图获得锁L2,而多线程中另外一个线程T_B持有锁L2并且试图申请获得锁L1。线程的锁申请操作是阻塞的,于是造成线程T_A和线程T_B无法正确获得想要的锁,两个线程被阻塞进入死锁状态。新Java线程Semaphore:并行环境下访问竞争资源控制_zhangphil的博客-CSDN博客。_semaphore会照成死锁吗https://blog.csdn.net/zhangphil/article/details/127547504新Java线程Semaphore:并行环境下访问竞争资源控制_zhangphil的博客-CSDN博客新Java线程Semaphore:并行环境下访问竞争资源控制Semaphore是从Java 1.5引入的Java线程新内容。Semaphore实现在线程的竞争资源访问环境下,对资源的访问控制。只有申请(acquire)得到Semaphore的许可证的线程任务可以访问竞争资源。例如: private void test() { // 虽然有很多线程想访问某些资源,但...https://blog.csdn.net/zhangphil/article/details/83410270

Java线程池:ExecutorService,Executors_executorservice线程池_zhangphil的博客-CSDN博客简单的Java线程池可以从Executors.newFixedThreadPool( int n)获得。此方法返回一个线程容量为n的线程池。然后ExecutorService的execute执行之。现给出一个示例。package zhangphil.executorservice;import java.util.concurrent.ExecutorService;import j_executorservice线程池https://blog.csdn.net/zhangphil/article/details/43898637

猜你喜欢

转载自blog.csdn.net/zhangphil/article/details/132165957