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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:11:12+00:00 2026-06-18T11:11:12+00:00

I am trying to measure how much time each thread takes in inserting to

  • 0

I am trying to measure how much time each thread takes in inserting to database. I have captured all those performance numbers in a ConcurrentHashMap named histogram like how much time each thread is taking in inserting.

Below is the code in which I am measuring how much time each thread is taking and storing it in a ConcurrentHashMap

class Task implements Runnable {

public static ConcurrentHashMap<Long, AtomicLong> histogram = new ConcurrentHashMap<Long, AtomicLong>();

     @Override
     public void run() {

     try {

     long start = System.nanoTime();

     preparedStatement.executeUpdate(); // flush the records.

     long end = System.nanoTime() - start;

     final AtomicLong before = histogram.putIfAbsent(end / 1000000, new AtomicLong(1L));
          if (before != null) {
               before.incrementAndGet();
          }

         }
     }

    }

So my question is whether the way I am trying to measure how much time each thread is taking and storing all those numbers in a ConcurrentHashMap will be thread safe or not?

I think my whole update operation is Atomic. And I just wanted to see if there are any better approach than this if my whole operation is not Atomic. I am looking for mostly lock free solution.

And then after every thread is finished executing there tasks, I am printing this Histogram map from the main method as I have made that map as Static. So this way is right or not?

public class LoadTest {

public static void main(String[] args) {

//executing all the threads using ExecutorService


//And then I am printing out the historgram that got created in Task class    
System.out.println(Task.histogram);

     }

}
  • 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-18T11:11:14+00:00Added an answer on June 18, 2026 at 11:11 am

    Your code is correct; there is also a (more complex) idiom that avoids instantiating AtomicLong each time. However do note that a “naïve” lock-based solution would probably be just as good due to a very low duration of the critical section.

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

Sidebar

Related Questions

I am trying to measure how much time each thread takes in inserting to
I am trying to check how much time does a function takes to execute
I am trying to measure the performance of Database Insert . So for that
I'm trying to measure how long it takes read then encrypt some data (independently).
I am trying to measure the performance increase of inlining an accessor into a
I'm trying to create a module/class in node.js to measure asynchronous execution time but
So I was trying to measure the time two different algorithm implementations took to
I am trying to measure the estimated cycle lengths it takes my computer to
I'm trying to measure the time difference between 2 signals on the parallel port,
Is there a way to measure the average time it takes my code to

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.