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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:53:37+00:00 2026-06-11T00:53:37+00:00

I have the main thread and ClientThread… In the main thread when a user

  • 0

I have the main thread and ClientThread…
In the main thread when a user connects to the server it calls a method in the ClientThread

    try {
        Socket socket = server.accept();
        clientThread.addClient(socket);
    } catch(Exception e) {
        e.printStackTrace();
    }

the method in the ClientThread adds content to an ArrayList

public void addClient(Socket socket) {
    clientSockets.add(socket);
}

The ClientThread also runs this code on every frame:

        for (Socket socket : clientSockets) {
            label.setText(socket.toString());
        }

for some reason i get this erron java.util.ConcurrentModificationException on this line for (Socket socket : clientSockets) {…

The question: why do i get this error, and how can i fix that?

  • 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-11T00:53:39+00:00Added an answer on June 11, 2026 at 12:53 am

    You need to synchronize access to clientSockets, since you are not allowed to change the structure of a List while iterating over it.

    A simple solution would be to make clientSockets a synchronized collection, and then synchronize on it explicitly before iteration:

    List<Socket> clientSockets = Collections.synchronizedList(new ArrayList<Socket>());
    
    
    //...when setting the labels:
    synchronized (clientSockets) {
        for ( Socket socket : clientSockets ) {
            label.setText(socket.toString());
        }
    }
    

    You would need to do the same whereever you iterate over the list.

    You can also consider using something like CopyOnArrayList, which does not require you to synchronize, at the expense of using more memory and potentially serving slightly stale data.

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

Sidebar

Related Questions

In my main thread I have a while(listening) loop which calls accept() on my
In the next examlpe I have Socket server handled in the thread. In the
I have main thread which calls another thread. timeout period of second one is
I have a main thread that invokes a child thread function at different times
I have the main thread from which I start a window using invokeLater .
In my application in C++ I am using pthreads. I have a main thread
I have a windows form on the main thread and another thread that does
I have a class class with main thread public class MainThread extends Thread {
I have code that reloads images via HTTP from the main thread and displays
I have to execute some code in the context of the main thread. I

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.