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 7821463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:39:39+00:00 2026-06-02T07:39:39+00:00

Here is some pseudo code as follows. public class MyObject { private List<Object> someStuff;

  • 0

Here is some pseudo code as follows.

public class MyObject
{   
    private List<Object> someStuff;
    private Timer timer;

    public MyObject()
    {
        someStuff = new ArrayList<Object>();

        timer = new Timer(new TimerTask(){

            public void run()
            {
                for(Object o : someStuff)
                {
                    //do some more stuff involving add and removes possibly
                }
            }
        }, 0, 60*1000);
    }

    public List<Object> getSomeStuff()
    {
        return this.someStuff;
    }
}

So essentially the problem is that other objects not listed in the code above call getSomeStuff() to get the list for read-only purposes. I am getting concurrentmodificationexception in the timer thread when this occurs. I tried making the getSomeStuff method synchronized, and even tried synchronized blocks in the timer thread, but still kept getting the error. What is the easiest method of stopping concurrent access of the list?

  • 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-06-02T07:39:40+00:00Added an answer on June 2, 2026 at 7:39 am

    You can use either java.util.concurrent.CopyOnWriteArrayList or make a copy (or get an array with Collection.toArray method) before iterating the list in the thread.

    Besides that, removing in a for-each construction breaks iterator, so it’s not a valid way to process the list in this case.

    But you can do the following:

    for (Iterator<SomeClass> i = list.iterator(); i.hasNext();) {
        SomeClass next = i.next();
        if (need_to_remove){
           i.remove(i);                
        }
    }
    

    or

    for (int i = list.size() - 1; i >= 0; i--){            
        if (need_to_remove) {
            list.remove(i);                
        }
    }
    

    Also note, that if your code accesses the list from different threads and the list is modified, you need to synchronize it. For example:

        private final ReadWriteLock lock = new ReentrantReadWriteLock();
    
    
        final Lock w = lock.writeLock();
        w.lock();
        try {
            // modifications of the list
        } finally {
            w.unlock();
        }
    
          .................................
    
        final Lock r = lock.readLock();
        r.lock();
        try {
            // read-only operations on the list
            // e.g. copy it to an array
        } finally {
            r.unlock();
        }
        // and iterate outside the lock 
    

    But note, that operations withing locks should be as short as possible.

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

Sidebar

Related Questions

So first here is some pseudo code of what I want to improve. public
I'm designing a method for an input handler class. Here is some pseudo code...
Here is some pseudo code of my setup: class IMyClass { ... }; //
Here's some code (full program follows later in the question): template <typename T> T
Here is some code: class Person def initialize(age) @age = age end def age
Here's some code: DirectorySearcher searcher = new DirectorySearcher(); searcher.Filter = (&(objectClass=user)(sAMAccountName= + lstUsers.SelectedItem.Text +
Here's some example code: class Obj attr :c, true def == that p '=='
Whew! Long title...here's some pseudo-code to explain that verbiage: int main(){ int* ptr =
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
here is some code I've been working on, basically I need to set up

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.