Question

I am trying to calculate taken my all the thread created by my program in total but my code is calculating the time only for invoking.

I am sure because in the output i see difference printed before all the threads are finished

below is my main program RequestProcessor here is my thread class

int i = 0;
List<Customer>customers = customer.getCustomers();
long startTimestamp = System.currentTimeMillis();
for (Customer c: customers){
    new RequestProcessor(c, i).start();
    i++;
}
long endTimestamp = System.currentTimeMillis();
System.out.println("difference  = "+(endTimestamp - startTimestamp));

below is the Request Processor class

public RequestProcessor( Customer customer , int threadSeq ) {
    super();
    this.customer = customer;
    this.threadSeq = threadSeq;
}

@Override
public void run() {
    // TODO Auto-generated method stub
    processRequest();
    super.run();
}

public void processRequest(){
    //performing execution on customer here
    System.out.println("processing "+ customer.getName()+" thread seq "+threadSeq);
}

below is the the output that I am getting

processing customer9 thread seq 9
processing customer7 thread seq 7
difference  = 4
processing customer3 thread seq 3
processing customer2 thread seq 2

I tried using join but didn't work for me

Was it helpful?

Solution

you can use CountDownLatch . it is a important concurrent tool class . if you want know more about java multithreaded program . see the book Java Concurrency in Practice [Brian Goetz]

 int i = 0;
  List<Customer>customers = customer.getCustomers();
  CountDownLatch cdl =new CountDownLatch(customers.size());      // new code 
  long startTimestamp = System.currentTimeMillis();

   for (Customer c: customers){
      new RequestProcessor(cdl,c, i).start();
      i++;
    }

   cdl.await();                                                      // new code . 
  // the thread will hang up unitl all RequestProcessor run cdl.countDown() ;
    long endTimestamp = System.currentTimeMillis();
    System.out.println("difference  = "+(endTimestamp - startTimestamp));

RequestProcessor.class

public RequestProcessor(CountDownLatch cdl, Customer customer , int threadSeq ) {
        super();
        this.customer = customer;
        this.threadSeq = threadSeq;
        this.cdl=cdl;                                            // new code 
        }
    @Override
    public void run() {
        // TODO Auto-generated method stub
       processRequest();
       super.run();
       cdl.countDown();                                          // new code 
    }

OTHER TIPS

You can use a CountDownLatch to wait until a certain number of threads call countDown(), at which point you can then print the running time. You'll need to alter your Customer class to take a CountDownLatch object so the countDown() method could be properly called, but once you do that this should work.

Something like:

CountDownLatch latch = new CountDownLatch(customers.size());
long startTimestamp = System.currentTimeMillis();
for (Customer c: customers){
    new RequestProcessor(c, i++, latch).start();
}
latch.await();
// have each thread call countDown() after it's done, etc.

You might need to do something a bit more complex like having two latches so you can get a better time by having one latch block each of the created threads until you call countDown() in the main thread, but that's the general idea.

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