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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:43:10+00:00 2026-05-27T12:43:10+00:00

I have a concurrent scenario where I have to write a lot to a

  • 0

I have a concurrent scenario where I have to write a lot to a sorted datastructure.

I thought about using ConcurrentSkipListMap for this reason. My definition is something like this: ConcurrentSkipListMap<K, List<V>>, which of course makes it quite difficult to manage insertions of the List<V> when the first element is inserted.

I.e.:

List<V> list = map.get(k);
if (list == null) {
    list = new LinkedList<V>();
    map.put(list);
}
list.add(v);

Of course this is not atomic. Using the class putIfAbsent() method would make it quite awkward and inefficient:

List<V> newElement = new LinkedList<V>();
List<V> previous = map.putIfAbsent(k, newElement);
if (previous != null) {
    previous.add(v);
} else {
    newElement.add(v);
}

One way is of course to create my own lock and protect a normal TreeMap, but as I have a real high write rate on this object, I’d prefer something designed specifically for it. Something like collections.defaultdict of python would be perfect, of course.

  • 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-27T12:43:11+00:00Added an answer on May 27, 2026 at 12:43 pm

    A couple things.

    First: The most effecient way to handle your the put-if-absent case is to do a pseudo double check

    public void add(Object key, Object val) {
        List list = map.get(key);
        if (list == null) {
            list = new LinkedList();
            List temp = map.putIfAbsent(list);
            if (temp != null)
                list = temp;
        }
        list.add(val);
    }
    

    That is as effecient as you can get for the put-if-absent case.

    Second: You still have a concurrency issue here with adding to the list. You may want to wrap the LinkedList in Collections.synchronizedList() before putting to the map.

    public void add(Object key, Object val) {
            List list = map.get(key);
            if (list == null) {
                list = Collections.synchronizedList(new LinkedList());
                List temp = map.putIfAbsent(list);
                if (temp != null)
                    list = temp;
            }
            list.add(val);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly specific question about concurrent programming in C. I have done
I have this administration site that is using (Fluent) NHibernate for data access. As
You have the following scenario: //Two threads, using shared data shared data = 2
I have been trying to solve this Concurrent Programming exam exercise (in C#): Knowing
We have a scenario One Main e-commerce website - currently attracting a lot of
I have created a concurrent, recursive directory traversal and file processing program, which sometimes
I have a multithreaded application that has many concurrent operations going on at once.
We have limit on number of concurrent sitecore users. We are facing a unique
I have multiple threads each one with its own private concurrent queue and all
I have a client which makes a limited number of concurrent web requests. I

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.