Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top