Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6942475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:01:18+00:00 2026-05-27T13:01:18+00:00

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

  • 0

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 then – later – another worker will grab a snapshot to persist to a datastore.

This is similar to the discussion over on this question.

There, the following solution is provided:

class CopyOnReadList<T> {

    private final List<T> items = new ArrayList<T>();

    public void add(T item) {
        synchronized (items) {
            // Add item while holding the lock.
            items.add(item);
        }
    }

    public List<T> makeSnapshot() {
        List<T> copy = new ArrayList<T>();
        synchronized (items) {
            // Make a copy while holding the lock.
            for (T t : items) copy.add(t);
        }
        return copy;
    }

}

However, in this scenario, (and, as I’ve learned from my question here), only one thread can write to the backing list at any given time.

Is there a way to allow high-concurrency writes to the backing list, which are locked only during the makeSnapshot() call?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:01:19+00:00Added an answer on May 27, 2026 at 1:01 pm

    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<T> items = new ArrayList<T>();
    
    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<T> makeSnapshot() {
        List<T> copy = new ArrayList<T>(), 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<Integer> 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multithreaded application that has many concurrent operations going on at once.
I have a multithreaded Windows application where one of the threads has a message
I have a situation where, in a multithreaded application, many different threads are accessing
Say I have a multithreaded application made up of two separate threads and a
If you have a multi-threaded application with a word variable shared between threads (ie
I have a multithreaded WPF application that is using > 600 threads after is
I have a multithreaded C# application where each thread has it's own set of
I have this code will work in multithreaded application. I know that immutable object
I have a multithreaded application, where each thread has a variable of integer type.
I have a class that contains a List(of T) used in a multithreaded application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.