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

  • Home
  • SEARCH
  • 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 4266668
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:47:32+00:00 2026-05-21T06:47:32+00:00

I have ArrayLists that store many objects, and objects are frequently added and removed

  • 0

I have ArrayLists that store many objects, and objects are frequently added and removed from the ArrayLists. One thread operates on the data structures and updates the ArrayList’s objects every 20ms or so. Another thread traverses the ArrayLists and uses their elements to paint the objects (also every 20-30ms).

If the ArrayLists are traversed using a for loop, IndexOutOfBoundsExceptions abound. If the ArrayLists are traversed using iterators, ConcurrentModificationExceptions abound. Synchronizing the ArrayLists like so:


List list = Collections.synchronizedList(new ArrayList());
synchronized(list) {
//use iterator for traversals
}

Throws no exceptions but has a substantial performance drain. Is there a way to traverse these ArrayLists without exceptions being throw, and without a performance drain?

THANKS!

  • 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-21T06:47:33+00:00Added an answer on May 21, 2026 at 6:47 am

    A good approach to this problem is to make threads work on different copies of the list. However, CopyOnWriteArrayList doesn’t fit here well, since it creates a copy at every modification, but in your case it would be better to create copies less frequent.

    So, you can implement it manually: the first thread creates a copy of the updated list and publishes it via the volatile variable, the second thread works with this copy (I assume that the first thread modifies only list, not objects in it):

    private volatile List publicList;
    
    // Thread A
    List originalList = ...;
    while (true) {
        modifyList(originalList); // Modify list
        publicList = new ArrayList(originalList); // Pusblish a copy
    }
    
    // Thread B
    while (true) {
        for (Object o: publicList) { // Iterate over a published copy
            ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that contains two arraylists which I'm trying to store objects
I have different objects which is used to store the data from DB in
I have 2 arraylists. One is principleList which contains integer values that denotes roles
In my case I have 2D ArrayList full of objects that have data which
I have an ArrayList that I want to use to hold RaceCar objects that
I have a stringtoList ArrayList that needs to return tokens from a StreamTokenizer but
I need to make an ArrayList of ArrayLists thread safe. I also cannot have
I have a query in Data Access Object DAOComments that joins users table and
I have roughly 420,000 elements that I need to store easily in a Set
OK, so I have a cursor adapter that I cobbled together from various source

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.