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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:48:05+00:00 2026-06-10T10:48:05+00:00

I have the following executing sequentially in the one thread: int size = hashTable.size();

  • 0

I have the following executing sequentially in the one thread:

int size = hashTable.size();

foreach…. in … hasTable.values()

do something

My question will the foreach be executed size times ? (even if another thread puts/removes an element meanwhile ?

  • 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-10T10:48:06+00:00Added an answer on June 10, 2026 at 10:48 am

    No, HashTable is thread-safe on method level (multiple threads can call any method at any time) but there is no cross-method synchronization. It’s possible that between your two instructions other thread adds/removes or even clears the hashtable.

    If you need to keep such invariant, make a defensive copy (doesn’t have to be thread safe) and do size()/loop on that copy:

    Map<K, V> map = null;
    synchronized(hashTable) {
      map = new java.util.HashMap<>(hashTable);
    }
    map.size();
    for(V v: map.values()) {
      //...
    }
    

    Here for-each is safe and it’s guaranteed to run size-times. Also as noted in comments, you can just synchronize on hashTable:

    synchronized(hashTable) {
      int size = hashTable.size();
      for(V v: hashTable.values()) {
        //...
      }
    }
    

    However this solution means that only one thread at a time can perform the loop (this can become a bottleneck if your loop takes some time to complete). With defensive copy each thread has its own copy and several threads can loop at the same time. On the other hand this solution is better if hashTable is very big (expensive to copy) but the iteration is very fast.

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

Sidebar

Related Questions

I have a problem with the following code, when executing the call to cublasSrotg
I have the following in one namespace say shapes : (derive ::rect ::shape) (derive
I m executing following: db2 -tvsf ./sql/update_product.sql I have used eclipse for editing update_product.sql
I have the following method that is executing twice every time it is called:
I have the following query that I'm executing using a Python script (by using
I have the following piece of code, executing a pretty simple MySQL query: $netnestquery
I have following in my HTML code: <div id=txtHint><b>Person info will be listed here.</b></div>
I have the following profile which I am executing successfully (mvn exec:exec -DrunMule): <profile>
I have the following for loop executing within my program and I can't see
I have following query where I am fetching Question count for exams created by

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.