T
Timo Nentwig
Hi!
I'm still not completely certain about the volatile keyword. Is it
actually required in the example below or would it only be required if
go() would access the fields of Stats directly (if they were
accessible, of course)?
Thanks
Timo
import java.util.Timer;
import java.util.TimerTask;
public class Test
{
class Stats
{
private volatile int counter = 0;
private volatile long cumulativeTime;
private volatile int min, avg, max;
public Stats( final int period )
{
reset();
final Timer timer = new Timer( true );
timer.schedule( new TimerTask()
{
public void run()
{
System.out.println( "i=" + counter + ", cumulative=" +
cumulativeTime );
synchronized( this )
{
avg = counter == 0 ? 0 : (int)(cumulativeTime / counter);
}
reset();
}
}, period, period );
}
private void reset()
{
synchronized( this )
{
counter = 0;
cumulativeTime = 0;
min = Integer.MAX_VALUE;
max = Integer.MIN_VALUE;
}
}
public void register( int t )
{
synchronized( this )
{
counter++;
cumulativeTime += t;
if( t < min ) min = t;
if( t > max ) max = t;
}
}
public int average()
{
return avg;
}
public int min()
{
return min;
}
public int max()
{
return max;
}
}
private static final int PERIOD = 2500;
private static final int INTERVAL = 1000;
public static void main( String[] args )
{
new Test().go();
}
private void go()
{
final Stats stats = new Stats( PERIOD );
final Timer reporter = new Timer();
reporter.schedule( new TimerTask()
{
public void run()
{
System.out.println( "avg=" + stats.average() + ", min=" +
stats.min() + ", max=" + stats.max() );
}
}, INTERVAL, INTERVAL );
while( true )
{
stats.register( (int)Math.round( Math.random() * 10000 ) );
try
{
Thread.sleep( (int)Math.round( Math.random() * 5 ) + 5 );
}
catch( InterruptedException e )
{
}
}
}
}
I'm still not completely certain about the volatile keyword. Is it
actually required in the example below or would it only be required if
go() would access the fields of Stats directly (if they were
accessible, of course)?
Thanks
Timo
import java.util.Timer;
import java.util.TimerTask;
public class Test
{
class Stats
{
private volatile int counter = 0;
private volatile long cumulativeTime;
private volatile int min, avg, max;
public Stats( final int period )
{
reset();
final Timer timer = new Timer( true );
timer.schedule( new TimerTask()
{
public void run()
{
System.out.println( "i=" + counter + ", cumulative=" +
cumulativeTime );
synchronized( this )
{
avg = counter == 0 ? 0 : (int)(cumulativeTime / counter);
}
reset();
}
}, period, period );
}
private void reset()
{
synchronized( this )
{
counter = 0;
cumulativeTime = 0;
min = Integer.MAX_VALUE;
max = Integer.MIN_VALUE;
}
}
public void register( int t )
{
synchronized( this )
{
counter++;
cumulativeTime += t;
if( t < min ) min = t;
if( t > max ) max = t;
}
}
public int average()
{
return avg;
}
public int min()
{
return min;
}
public int max()
{
return max;
}
}
private static final int PERIOD = 2500;
private static final int INTERVAL = 1000;
public static void main( String[] args )
{
new Test().go();
}
private void go()
{
final Stats stats = new Stats( PERIOD );
final Timer reporter = new Timer();
reporter.schedule( new TimerTask()
{
public void run()
{
System.out.println( "avg=" + stats.average() + ", min=" +
stats.min() + ", max=" + stats.max() );
}
}, INTERVAL, INTERVAL );
while( true )
{
stats.register( (int)Math.round( Math.random() * 10000 ) );
try
{
Thread.sleep( (int)Math.round( Math.random() * 5 ) + 5 );
}
catch( InterruptedException e )
{
}
}
}
}