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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:04:33+00:00 2026-06-17T09:04:33+00:00

I have a class (lets call it XClass) which has a method (lets call

  • 0

I have a class (lets call it XClass) which has a method (lets call it xMethod), that I am testing. It also contains a:

private static Map<String, String> map = new HashMap<String, String>();

In my setup method of the unit test I have:

ReflectionTestUtils.setField(xClass, "map", map, null);

In the test method I create couple of (in my case 8) threads. Their run method invokes xClass.xMethod. This method changes the static map variable. The method xMethod, is supposed to invoke map.containsKey() map.get() and map.put() 8 times. It doesn’t do any remove. This method also doesn’t create any new threads, so the map shouldn’t be changed once the thread is finished with the xMethod. I wait for all of the threads to finish (either normally or by an exception). Than I check the map

int mapSize = map.size();
assertEquals("map:" + map, 8, mapSize);

It fails here with the message:

java.lang.AssertionError: map:{3=x1, 2=x2, 1=x3, 7=x4, 6=x5, 5=x6,
4=x7, 8=x8} expected:<8> but was:<7>

I solved the problem by using a ConcurrentHashMap, but I am still puzzled with the issue. How is it possible after all 8 threads finish, for the map to behave weird (size() returns 7 but toString() prints 8 entities)? I would have understood if there were 7 entites and the size() method gave 7, but there are 8 entities in the map. How is this possible?!

Btw, I checked the termination of the threads in couple of ways:
1. with a check for the Thread.State.TERMINATED.
2. I printed a simple message before the method return, printed a simple message after the threads were done (and checked there were no exceptions thrown. The 8 messages are always written before the 9th (in the test after threads “finished”).
3. Even done a simple thread which in the run method contained the following logic:

        public void run() {
                try {
                        obj = xClass.xMethod();
                } catch (Exception e) {
                        exc = e;
                }
                finished = true;
        }

Than I would only loop indefinitely until all of the threads had their finished == true.

All of these imply that the threads are finished before I continue with asserting and checking the map. So how, again, is it possible for the map.size() to return 7 and the map.toString() to return 8 entities?!

Kind Regards,
despot

  • 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-17T09:04:34+00:00Added an answer on June 17, 2026 at 9:04 am

    By concurrently mutating the map you have disrupted its internal invariants and you are observing unspecified and unpredictable behavior, which is the expected result.

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

Sidebar

Related Questions

I have a class that has a complex static factory method, lets call it
I have an ActiveRecord class - lets call it comments - which has an
I have a layer based class that contains two sprites as children. Lets call
I have a table, which generated to class, lets call it MyClass ,which has
I have a class A which has a private method called a(). I have
I have a class annotated with @Configuration (let's call it StubConfiguration ) which has
I have a class that extends Zend_Db_Table lets call it 'Users' that uses the
I have a UIImageView based class (lets call it classA) that uses a classB
I have a class of mine, lets call it TMyObject, which should return a
I have this class mapped as a entity, lets call it Person. Person 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.