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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:39:53+00:00 2026-06-18T02:39:53+00:00

I want to know if I need to measure time elapsed then Single Threaded

  • 0

I want to know if I need to measure time elapsed then Single Threaded Program is good approach or Multithreading Program is a good approach for that.

Below is my single threaded program that is measuring the time of our service-

private static void serviceCall() {

    histogram = new HashMap<Long, Long>();
    keys = histogram.keySet();
    long total = 5;
    long runs = total;

    while (runs > 0) {

        long start_time = System.currentTimeMillis();
        result = restTemplate.getForObject("SOME URL",String.class);
        long difference = (System.currentTimeMillis() - start_time);

        Long count = histogram.get(difference);
        if (count != null) {
            count++;
            histogram.put(Long.valueOf(difference), count);
        } else {
            histogram.put(Long.valueOf(difference), Long.valueOf(1L));
        }
        runs--;
    }

    for (Long key : keys) {
        Long value = histogram.get(key);
        System.out.println("MEASUREMENT " + key + ":" + value);
    }
}

Output I get from this Single Threaded Program is- Total call was 5

MEASUREMENT 163:1
MEASUREMENT 42:3
MEASUREMENT 47:1

which means 1 call came back in 163 ms. 3 calls came back in 42 ms and so on.

And also I did tried using Multithreaded program as well to measure the time elapsed. Meaning hitting the service parallely with few threads and then measuring how much each thread is taking.

Below is the code for that as well-

//create thread pool with given size 
    ExecutorService service = Executors.newFixedThreadPool(10);

// queue some tasks 
for (int i = 0; i < 1 * 5; i++) {
    service.submit(new ThreadTask(i, histogram));
}


public ThreadTask(int id, HashMap<Long, Long> histogram) {
    this.id = id;
    this.hg = histogram;
}


@Override
public void run() {

    long start_time = System.currentTimeMillis();

    result = restTemplate.getForObject("",  String.class);
    long difference = (System.currentTimeMillis() - start_time);

    Long count = hg.get(difference);
    if (count != null) {
        count++;
        hg.put(Long.valueOf(difference), count);
    } else {
        hg.put(Long.valueOf(difference), Long.valueOf(1L));
    }

}

And below is the result I get from the above program-

{176=1, 213=1, 182=1, 136=1, 155=1}

One call came back in 176 ms, and so on

So my question is why Multithreading program is taking a lot more time as compared to above Single threaded program? If there is some loop hole in my Multithreading program, can anyone help me to improve it?

  • 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-18T02:39:55+00:00Added an answer on June 18, 2026 at 2:39 am

    Your multi-threaded program likely makes all the requests at the same time which puts more strain on the server which will cause it to respond slower to all request.

    As an aside, the way you are doing the update isn’t threadsafe, so your count will likely be off in the multithreaded scenario given enough trials.

    For instance, Thread A and B both return in 100 ms at the same time. The count in histogram for 100 is 3. A gets 3. B gets 3. A updates 3 to 4. B updates 3 to 4. A puts the value 4 in the histogram. B puts the value 4 in the histogram. You’ve now had 2 threads believe they incremented the count but the count in the histogram only reflects being incremented once.

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

Sidebar

Related Questions

I need some comments on the code that I assembled. I want to know
HI, i need a XML parser. i want to know that which one is
I'm using windows 7 64 bits and I want to know if I need
I want to develop an application in which I need to know the position
When I want to design UI and I need to know what are predefined
I just want to follow conventions and i need to know where do i
I have made a file called time.hs . It contains a single function that
I have the following code for which I want to know if I need
I have downloaded android-ndk-r8b.zip(180MB),but I want to know if I need to download anything
I have got some CTab items, which I want to measure, especially I need

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.