Java concurrency - improving a copy-on-read collection

后端 未结 5 924
梦毁少年i
梦毁少年i 2021-01-07 06:15

I have a multithreaded application, where a shared list has write-often, read-occasionally behaviour.

Specifically, many threads will dump data into the list, and th

5条回答
  •  情深已故
    2021-01-07 06:34

    synchronized (~20 ns) is pretty fast and even though other operations can allow concurrency, they can be slower.

    private final Lock lock = new ReentrantLock();
    private List items = new ArrayList();
    
    public void add(T item) {
        lock.lock();
        // trivial lock time.
        try {
            // Add item while holding the lock.
            items.add(item);
        } finally {
            lock.unlock();
        }
    }
    
    public List makeSnapshot() {
        List copy = new ArrayList(), ret;
        lock.lock();
        // trivial lock time.
        try {
            ret = items;
            items = copy;
        } finally {
            lock.unlock();
        }
        return ret;
    }
    
    public static void main(String... args) {
        long start = System.nanoTime();
        Main ints = new Main<>();
        for (int j = 0; j < 100 * 1000; j++) {
            for (int i = 0; i < 1000; i++)
                ints.add(i);
            ints.makeSnapshot();
        }
        long time = System.nanoTime() - start;
        System.out.printf("The average time to add was %,d ns%n", time / 100 / 1000 / 1000);
    }
    

    prints

    The average time to add was 28 ns
    

    This means if you are creating 30 million entries per second, you will have one thread accessing the list on average. If you are creating 60 million per second, you will have concurrency issues, however you are likely to be having many more resourcing issue at this point.

    Using Lock.lock() and Lock.unlock() can be faster when there is a high contention ratio. However, I suspect your threads will be spending most of the time building the objects to be created rather than waiting to add the objects.

提交回复
热议问题