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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:30:03+00:00 2026-05-17T18:30:03+00:00

I’m writing some client-server-application where I have to deal with multiple threads. I’ve got

  • 0

I’m writing some client-server-application where I have to deal with multiple threads. I’ve got some servers, that send alive-packets every few seconds. Those servers are maintained in a ConcurrentHashMap, that contains their EndPoints paired with the time the last alive-package arrived of the respective server.

Now I’ve got a thread, that has to “sort out” all the servers that haven’t sent alive-packets for a specific amount of time.

I guess I can’t just do it like that, can I?

for( IPEndPoint server : this.fileservers.keySet() )
{
    Long time = this.fileservers.get( server );

    //If server's time is updated here, I got a problem

    if( time > fileserverTimeout )
        this.fileservers.remove( server );
}

Is there a way I can get around that without aquiring a lock for the whole loop (that I then have to respect in the other threads as well)?

  • 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-17T18:30:04+00:00Added an answer on May 17, 2026 at 6:30 pm

    There is probably no problem here, depending on what exactly you store in the map. Your code looks a little weird to me, since you seem to save “the duration for which the server hasn’t been active”.

    My first idea for recording that data was to store “the latest timestamp at which the server has been active”. Then your code would look like this:

    package so3950354;
    
    import java.util.Iterator;
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.concurrent.ConcurrentMap;
    
    public class ServerManager {
    
      private final ConcurrentMap<Server, Long> lastActive = new ConcurrentHashMap<Server, Long>();
    
      /** May be overridden by a special method for testing. */
      protected long now() {
        return System.currentTimeMillis();
      }
    
      public void markActive(Server server) {
        lastActive.put(server, Long.valueOf(now()));
      }
    
      public void removeInactive(long timeoutMillis) {
        final long now = now();
    
        Iterator<Map.Entry<Server, Long>> it = lastActive.entrySet().iterator();
        while (it.hasNext()) {
          final Map.Entry<Server, Long> entry = it.next();
          final long backThen = entry.getValue().longValue();
          /*
           * Even if some other code updates the timestamp of this server now,
           * the server had timed out at some point in time, so it may be
           * removed. It's bad luck, but impossible to avoid.
           */
          if (now - backThen >= timeoutMillis) {
            it.remove();
          }
        }
      }
    
      static class Server {
    
      }
    }
    

    If you really want to avoid that no code ever calls markActive during a call to removeInactive, there is no way around explicit locking. What you probably want is:

    • concurrent calls to markActive are allowed.
    • during markActive no calls to removeInactive are allowed.
    • during removeInactive no calls to markActive are allowed.

    This looks like a typical scenario for a ReadWriteLock, where markActive is the “reading” operation and removeInactive is the “writing” Operation.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.