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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:28:35+00:00 2026-06-12T08:28:35+00:00

I know this is a daft questions but cant work out how to fix

  • 0

I know this is a daft questions but cant work out how to fix this, I have not had much experience with using threads before.

Below should create first of all a timer which will execute the command output.write(mylist) every 10 seconds which will simply output the contents of mylist.

Secondly it loops through about 3 lists I have and for each them creates a thread which will continue to loop through getting the next word in the list.
Please note: this is stripped down version and not complete so please don’t comment on the arraylist / list but rather the error itself.

There is a concurrent modification exception happening frequently but not all the time when it tries to do output.write(). I am guessing this is because one of the other threads are currently saving something to mylist? How would I go about fixing this?

    Runnable timer = new Runnable() {
        public void run() {
            try {
                while (true) {
                    Thread.sleep(10000);
                    output.write(mylist);
                }
            } catch (InterruptedException iex) {}
        }
    };
    Thread timerThread = new Thread(timer);
    timerThread.start();

    for (final ValueList item : list) {

        Runnable r = new Runnable() {
            public void run() {
                try {
                    while (true) {

                        char curr = item.getNext();

                         mylist.addWord(curr);
                    }
                } catch (InterruptedException iex) {}
            }
        };

        Thread thr = new Thread(r);
        thr.start();
    }
}
  • 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-12T08:28:36+00:00Added an answer on June 12, 2026 at 8:28 am

    There is a concurrent modification exception happening frequently but not all the time when it tries to do output.write…

    The problem is (I assume) that the output.write(...) method is iterating through your myList at the same time as another thread is calling myList.addWord(curr);. This is not allowed unless myList is a concurrent collection.

    How would I go about fixing this?

    You will need to synchronize on myList every time you are accessing it — in this case when you are outputting it or adding a word to it.

      synchronized (myList) {
          output.write(mylist);
      }
      ...
    
      synchronized (myList) {
          myList.addWord(curr);
     }
    

    In this case, because output.write(mylist) is probably iterating through the list, you cannot use the Collections.synchronizedList(...) method because the iterators need to be synchronized by the caller.

    If this is some high-performance method that is called tons of times then you could also use a ConcurrentLinkedQueue but that is a queue, not a list obviously. ConcurrentSkipList is another option although that is a heavier data structure.

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

Sidebar

Related Questions

I know this question has been asked before but the solutions did not work
I know this sounds very dumb, but we have a client that sends out
I know this is a daft question but I have a select box with
I know this has been asked a million times, but I haven't had much
Know this might be rather basic, but I been trying to figure out how
I know this is an old question, but I have spend any hours on
I know this is easy but i m just not able to debug it.
I know this question would have been asked several time but still I am
I know this is similar to many other questions on S.O. but none of
I know this is a stupid question, but I have been teaching myself c#,

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.