• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Measuring Time in Multi-threaded Application Using "System.currentTimeMillis()"

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Recently, I have found out an interesting problem in measuring the time in a multi-threaded application. What the application does is that it creates multiple threads each of which performs HTTP POST a constant number of times. The problem is that each of the threads reports the average time that increases from one thread to another. This problem does not exist with one thread in which the average time is measured. I have observed this problem with JDK v1.4.2 and JDK v1.5.0 on MS Windows 2000. I have not had any chance to run the same code on other operating systems, i.e. Linux or AIX. For some reason, "System.currentTimeMillis()" does not seem to work correctly on MS Windows 2000.

Has anyone of you ever observed this behavior on MS Windows 2000 before? If yes, is there a solution/work-around for this problem?

BTW, attached is the source code of the custom thread below.

Thanks

Regards

Bon

========================================================================


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.Header;

public class Worker extends Thread
{
private String soapMessage = null;
private int numberOfInvocations = 0;
private int thinkTime = 0;
private double totalTime = 0;
private String errorMessage = null;
private String soapAction = null;
private String url = null;
private HttpClient httpClient = null;
private PostMethod postMethod = null;

public HttpClientThreadWorker(String soapMessage, int numberOfInvocations, int thinkTime, String soapAction, String url)
{
this.soapMessage = soapMessage;
this.numberOfInvocations = numberOfInvocations;
this.thinkTime = thinkTime;
this.soapAction = soapAction;
this.url = url;
}

public void run()
{
int result = -1;
totalTime = 0;

try
{
httpClient = new HttpClient();
postMethod = new PostMethod(url);

if(soapAction != null)
{
postMethod.setRequestHeader(new Header("SOAPAction", soapAction));
}

postMethod.setRequestEntity(new StringRequestEntity(soapMessage));
postMethod.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");

long startTime = System.currentTimeMillis();

for(int i = 0; i < numberOfInvocations; i++)
{
result = httpClient.executeMethod(postMethod);
if(thinkTime > 0)
{
sleep(((long)thinkTime * 1000));
}
}

long endTime = System.currentTimeMillis();
totalTime = (double)(endTime - startTime);
System.out.println("Total time per thread (s): " + (totalTime/(double)1000));
System.out.println("Average time per invocation (s): " + totalTime/((double)1000 * numberOfInvocations));
}
catch(Exception ex)
{
ex.printStackTrace();
errorMessage = ex.toString();
}
finally
{
if(postMethod != null)
{
postMethod.releaseConnection();
postMethod = null;
}
httpClient = null;
}

}

/**
*
*This method retuns the total time.
*
*@return the total time
*/
public double getTotalTime()
{
return totalTime;
}

/**
*
*This method retuns the error message.
*
*@return the error message
*/
public String getErrorMessage()
{
return errorMessage;
}
}
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bon Truong:
The problem is that each of the threads reports the average time that increases from one thread to another. This problem does not exist with one thread in which the average time is measured.



I don't understand what the problem is. How is it reporting time incorrectly? It's going to increase as you spawn more Threads because there's more competition for the resources. You do understand that 10 Threads on one processor can't do more work than 1 Thread on one processor unless there's a resource bottleneck (such as I/O) somewhere right?
 
author
Posts: 4354
45
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bon Truong:
Hello,
The problem is that each of the threads reports the average time that increases from one thread to another. This problem does not exist with one thread in which the average time is measured.



There's absolutely nothing to guarentee which thread will get executed when, so one thread may take the CPU for a huge chunk of time and block all other threads or they could constantly switch back and forth between threads. The CPU scheduler will *TRY* to balance time between threads especially if the thread does not need the CPU, but the behavior can be extremely unpredictable especially in java threads, since there are JVM issues as well.
 
Everybody! Do the Funky Monkey! Like this tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic