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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:17:39+00:00 2026-06-13T15:17:39+00:00

I have a static method which I am calling from an Asynctask in doInBackGround()

  • 0

I have a static method which I am calling from an Asynctask in doInBackGround()

In the method there is this part of code:

    ArrayList<Message> messagesList = new ArrayList<Message>();
    if (!clearList) {
        messagesList.addAll(messages.getMessagesList());
            for (Message msg : messagesList) {
                if (msg.getId().length() == 0) {
                    messagesList.remove(msg);
                }
        }
    }

It is sometimes throwing ‘Concurrent modification exception’, I have tried declaring the method as ‘synchronized’ but it still didn’t help, and I cannot declare the block synchronized, since it is a static method and there is no ‘this’ reference.

I have also tried to stop a running asynctask if I need to start another one, but it didn’t help as well.

Help appreciated.

  • 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-13T15:17:40+00:00Added an answer on June 13, 2026 at 3:17 pm

    This has nothing to do with synchronization. You’re using an iterator to loop over messagesList, but then using remove to modify it during the iteration. You can’t do that, because ArrayList‘s iterators fail when the list if modified during iteration. From the docs:

    The iterators returned by this class’s iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove or add methods, the iterator will throw a ConcurrentModificationException.

    Your enhanced for loop is just syntactic sugar around using an Iterator, so you can just make that explicit and then use the iterator’s remove method:

    Iterator<Message> it = messagesList.iterator();
    while (it.hasNext()) {
        if (it.next().getId().length == 0) {
           it.remove();
        }
    }
    

    Alternately, you can just use a simple for loop running backward and indexing into the ArrayList (since get(int) is a cheap and constant-time operation on an ArrayList, which isn’t true of all Lists):

    int index;
    for (index = messagesList.length - 1; index >= 0; --index) {
        if (messagesList.get(index).getId().length == 0) {
           messagesList.remove(index);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have static method which returns me as it's name says data from domain
I have a problem calling a static method from a class. Let me explain
I have a PHP class method which is using a static function from with
I have a static web method which returns an object and this object has
I have a static method which sets a variable: static String[] playersNames; public static
I have a class with static method which has a local static variable. I
I have a method which i want to convert to Extension Method public static
I have a static method in my code that I would like somehow to
I have a static helper clone method of a specific base type which doesn't
I have created a console application in C# and there is main method (static)

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.