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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:06:53+00:00 2026-06-13T00:06:53+00:00

This question is based on Synchronizing on an Integer value . The solution there

  • 0

This question is based on Synchronizing on an Integer value.

The solution there seems excellent only there is small problem it does not address the concern how to delete values from ConcurrentHashMap.

So to address that I did below program

import java.util.concurrent.ConcurrentHashMap;

public class Example {

    private final ConcurrentHashMap<Integer, Integer> concurrentHashMap = new ConcurrentHashMap<Integer, Integer>();

    public void doSomething(int i) {
        synchronized (getLockForId(i)) {
            concurrentHashMap.remove(i);
        }
    }

    public Integer getLockForId(int id) {
        concurrentHashMap.putIfAbsent(id, id); // I want to replace these two
                                                // operation with single one
                                                // since it seems the cause of
                                                // NPE
        return concurrentHashMap.get(id);
    }

    public static void main(String[] args) {
        final Example example = new Example();
        new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (true) {
                    example.doSomething(++i);
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (true) {
                    example.doSomething(++i);
                }
            }
        }).start();
    }
}

Problem is that it always results in NullPointerException. My first analysis was because I am deleting the value it gets assigned to null so it is causing NullPointerException. So I did below

    Object obj = new Object();
    synchronized (obj) {
        obj = null;
    }

But above does not result in NullPointerException. So My question is why it is throwing NullPointerException in above case?

Even if I do

public Integer getLockForId(int id) {
   return concurrentHashMap.putIfAbsent(id, id); 
}

It still results in NullPointerException because it only returns value when there is one else return null

  • 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-13T00:06:53+00:00Added an answer on June 13, 2026 at 12:06 am

    Well yes, that would throw a NullPointerException. Consider this pattern:

     Thread 1                     Thread 2
    
     putIfAbsent (puts)
     get (returns non-null)
     acquire monitor
                                  putIfAbsent (doesn't put)
     remove (removes value)
                                  get (returns null)
                                  acquire monitor (bang!)
    

    It’s not that the “value get assigned to null” – it’s that Map.get returns null if there’s no entry for the given key.

    It’s hard to know what to recommend, as your code really doesn’t do anything useful. If you can say what you’re trying to achieve in your real code, we can give you better suggestions, potentially.

    EDIT: As noted by Nikita, just returning the value of putIfAbsent doesn’t work, as that returns the previous value, or null if it was absent – whereas you want the new value for the entry.

    I suspect you’ll have to synchronize access to the map, basically, to make your getLockId method atomic with respect to the remove operation.

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

Sidebar

Related Questions

This question is based on this thread . Problem: to access MySQL's manual when
Based on this question Fade in/out text based upon value of counter I have
EDIT : Re-written this question based on original answer The scala.collection.immutable.Set class is not
This question is based on this older one (not by me). I need to
(Note: Please do not mark this question as duplicate. This question is based on
This question is based on this answer . What does the following sentence mean?
This question is about testable software design based on mostly value objects and services.
This question is based on my previous question which I got a working answer
This question is based on the thread . I have the shell function function
This question is based on this thread . Is [---] a comment in Git

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.