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

The Archive Base Latest Questions

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

I wrote a stock market simulator which uses a ConcurrentHashMap as a cache. The

  • 0

I wrote a stock market simulator which uses a ConcurrentHashMap as a cache.

The cache holds about 75 elements but they are updated and retrieved very quickly (~ 500 times a second).

Here is what I did:

Thread 1:

Connected to an outside system which provides me with streaming quotes for a given stock symbol.

Thread 2 (callback thread):

Waits till data is delivered to it by the outside system. Once it gets the data, it parses it, creates an immutable DataEntry object, caches it and sends a signal to thread3.

Thread 3 (Consumer thread):
Upon receiving the signal, retrieve the DataEntry from the cache and uses it. (It is part of the task to not let thread2 push data directly to thread3).

public final class DataEntry{

      private final String field1;
      private final String field2;
      //...
      private final String field25;

      // Corresponding setters and getters

}

public final class Cache{

        private final Map<String, DataEntry> cache;

        public Cache( ){
           this.cache = new ConcurrentHashMap<String, DataEntry> ( 65, 0.75, 32 );
        }

        // Methods to update and retrieve DataEntry from the cache.
}

After running it through a profiler, I noticed that I am creating a lot of DataEntry object. And therefore eden is filling up very quickly.

So, I am thinking of tweaking the design a bit by:

a) Making the DataEntry class mutable.

b) Pre-populating the cache with empty DataEntry objects.

c) When the update arrives, retrieve the DataEntry object from the map and populate the fields.

This way, number of DataEntry object will be constant and equal to the number of elements.

My questions are:

a) Does this design have any concurrency issues that I may have introduced by making the DataEntry mutable.

b) Is there anything else I can do to optimize the cache?

Thanks.

  • 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-27T18:08:10+00:00Added an answer on May 27, 2026 at 6:08 pm

    I wouldn’t worry about the speed of ConcurrentHashMap

    Map<Integer, Integer> map = new ConcurrentHashMap<>();
    long start = System.nanoTime();
    int runs = 200*1000*1000;
    for (int r = 0; r < runs; r++) {
        map.put(r & 127, r & 127);
        map.get((~r) & 127);
    }
    long time = System.nanoTime() - start;
    System.out.printf("Throughput of %.1f million accesses per second%n",
            2 * runs / 1e6 / (time / 1e9));
    

    prints

    Throughput of 72.6 million accesses per second
    

    This is far beyond the access rate you appear to be using.

    If you want to reduce garbage you can use mutable objects and primitive. For this reason I would avoid using String (as you appear to have far more strings than data entries)

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

Sidebar

Related Questions

I wrote a function that uses YQL to pull stock info as follows: function
I wrote some small apps using .NET 3.5 but now I am stuck with
So, it's been about 3 years since I wrote and went live with my
I wrote a C program in which I did some pretty heavy stack allocation,
I wrote this snippet of code and I assume len is tail-recursive, but a
I wrote a program which solves some kind of optimization problem. I have been
I wrote an app which has 2 screens. The first screen is triggered by
I wrote a simple program which takes a predefined number to a predefined power
I recently wrote some Javascript code to generate random fake stock data as I
I wrote the following code to retrieve data about stocks in the S&P 500.

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.