System.nanoTimer()
I upgraded to JDK 1.5 and I used theSystem.nanoTimer() method to determine the running time of my algorithm sinceSystem.currentTimeMillis() methos was giving odd results.
However when I complie the code it says "cannot find symbol - method nanoTimer()".
Does anyone have any reasons why? Maybe my coding is incorrect?
Here is my coding:
publicclass StopWatch
{
/**
Constructs a stopwatch that is in the stopped state
and has no time accumulated.
*/
public StopWatch()
{
reset();
}
/**
Starts the stopwatch. Time starts accumulating now.
*/
publicvoid start()
{
if (isRunning)return;
isRunning =true;
startTime = System.nanoTimer();
}
/**
Stops the stopwatch. Time stops accumulating and is
is added to the elapsed time.
*/
publicvoid stop()
{
if (!isRunning)return;
isRunning =false;
long endTime = System.nanoTimer() ;
elapsedTime = elapsedTime + endTime - startTime;
}
/**
Returns the total elapsed time.
:@return the total elapsed time
*/
publiclong getElapsedTime()
{
if (isRunning)
{
long endTime = System.nanoTimer();
return elapsedTime + endTime - startTime;
}
else
return elapsedTime;
}
/**
Stops the watch and resets the elapsed time to 0.
*/
publicvoid reset()
{
elapsedTime = 0;
isRunning =false;
}
privatelong elapsedTime;
privatelong startTime;
privateboolean isRunning;
}

