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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:38:27+00:00 2026-05-30T13:38:27+00:00

I have a client-server style design. What I have done is create a class

  • 0

I have a client-server style design. What I have done is create a class called RequestController that controls and monitors all server requests made as a threaded object.

These request threads are tracked in a synchronized Map (created with Collections.synchronizedMap(new HashMap<Long, RequestThread>())). Every half second each request thread is checked, if it is still alive it sends a progress message to its interested listener object (an attribute of the request thread) if it has completed or failed it notifies the interested listener and removes itself from the synchronized Map when its run() method completes.

The RequestThread object that I use to track each request is a protected inner class of the RequestController object that is monitoring all the requests. So it has access to the map that contains it.

It is here that I am having problems. I get concurrent modification exceptions when the “dead thread” is removed from the map. All my access to the map is synchronized so I don’t understand how I can get an exception.

Here is the control loop code:

this.reqTimer = new Timer("Request Timer", true);
TimerTask reqTask = new TimerTask()
{
    @Override
    public void run()
    {
        synchronized(reqList)
        {
            for(RequestThread rt : reqList.values())
                rt.updateProgress();
        }
    }
};
this.reqTimer.scheduleAtFixedRate(reqTask, 500L, 500L);

At the very end of the RequestThread.run() method there is call to the RequestController method to remove itself from the map like so:

   public void removeRequest(long id)
   {
       synchronized(this.reqList)
       {
           this.reqList.remove(id);
       }
   }

It appears that if a RequestThread.run() completes while the TimerTask.run() method is looping through the map of requests to invoke the updateProgress() method the remove is not blocked but is allowed to alter the map and cause my exception. How can two different threads get a lock on the same object at the same time? Shouldn’t the remove be blocked because it originates in a different thread until the update is complete?

  • 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-30T13:38:29+00:00Added an answer on May 30, 2026 at 1:38 pm

    I’m going to turn my comments into an answer.

    How can two different threads get a lock on the same object at the same time?

    They can’t. The concurrent modification must be somewhere else. I suspect that your updateProgress() is what is doing the removal from the list. That may be where the concurrent modification exception is happening.

    for (RequestThread rt : reqList.values()) {
        // you can't make any changes to reqList inside of the loop
        rt.updateProgress();
    }
    

    If you need to have updateProgress() remove the request from the list then you could either:

    • Use an iterator and pass the iterator into updateProgress() so it can call iterator.remove().
    • Pass in a List<RequestThread> to updateProgress() and add the requests that are to be removed. Then once you are outside of the for loop (but still in the synchronized block) you can remove the items in your remove list from reqList.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a client/server style application which communicates using WCF which all works great.
I have a client/server application that communicates with .Net remoting. I need my clients
I have a client server application that sends XML over TCP/IP from client to
I have a client-server application that uses .net remoting. The clients are in a
I have a client server based windows forms application that needs an administrator only
I have WYSIWYG style javascript interface that allows a designer to create rectangular regions
I have to create a client/server system to stream video and audio. It would
I currently have a client-server application that involves a Silverlight client and a .NET
I have client/server applications and a very simple protocol for communication. More precisely, it's
We have client server based app which saves user related data into a zip

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.