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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:52:09+00:00 2026-06-17T22:52:09+00:00

In case of following code, run by multiple threads: private static final Map<String, keyinfo>

  • 0

In case of following code, run by multiple threads:

private static final Map<String, keyinfo> mapKeys = new ConcurrentHashMap<String, keyinfo>();

private static void purgeOldKeys() {
    for (Map.Entry<String, keyinfo> key : mapKeys.entrySet()) {
        if(key.getValue().createTime + keyCacheTime < getCurrentDBTime())
            mapKeys.remove(key);
    }
}

Can I avoid the synchronizer?

Or because removing already removed element, is not defined according to JavaDoc, the synchronizer will be still required?

  • 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-17T22:52:10+00:00Added an answer on June 17, 2026 at 10:52 pm

    In general, when removing from a collection, it is much cleaner (and faster!) to use a full Iterator API instead of the lazy “foreach” notion.

    The iterator.remove(); will not invalidate the iterator; and it knows the position it was at. Use this pattern:

    for (Iterator<> iter = map.entrySet().iterator(); iter.hasNext(); ) {
        Map.Entry<> entry = iter.next();
        if (testRemoval(entry))
            iter.remove(); // <----- remove using the iterator position!
    }
    

    It’s faster because it does not involve searching the object again. It’s more robust, because the iterator knows the object has been removed. In many collections, the code you showed above will “fast fail” because of a concurrent modification.

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

Sidebar

Related Questions

Suppose I have the following code: case class Foo(x: SortedSet[String]) { def bar: Set[String]
The following code will output the command string I wish to run: [string] $SourceRepo=C:\inetpub\wwwroot\Spyda\
I run the following code StringBuilder sb = new StringBuilder(); Stack stack = new
The following code looks at any element with the title-case class and modifies the
Is the following code the case of legal forward referencing? if yes why? public
I have the following code (in this case for two images - but in
I have the following code in test.sh: while getopts f:i: opt; do case $opt
I have the following code: .h NSString *mainString; .m case 0: case 1: case
I have code like following: switch(sort.Column) { case code: model = (sort.Direction == SortDirection.Ascending)
I have the following snippet from my code: switch ($extention) { case gif: $src

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.