volatile



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 )
{
}
}

}
}

.



Relevant Pages

  • Re: Sortable Java Tree Table
    ... I have never actually done this, but if I wanted to sort a JTree, ... private Listener listener = new Listener; ... public Object getChild(Object parent, int index) { ... public void valueForPathChanged{ ...
    (comp.lang.java.gui)
  • poker odds
    ... public static final int HIGH_CARD = 0; ... private static Logger logger; ... public Poker(int numCard, Card deck) ... public void maxSimulation ...
    (comp.lang.java.programmer)
  • Re: NotSerializable object issue...
    ... private static final NoopOutputStream DUMMY_OUTPUT_STREAM = new ... public SerializableCheckerthrows IOException { ... public void writeObjectthrows IOException { ... private int counter; ...
    (comp.lang.java.programmer)
  • Re: Im needing help with compile errors for GUI enviroment GameOfWar
    ... public static int[] randomize ... private Rank ... public void paintComponent ... class WinningsPanel extends JPanel implements ItemListener ...
    (comp.lang.java.gui)
  • Re: Im needing help with compile errors GameOfWar new compile errors under 2judith
    ... public static int[] randomize ... private Rank ... public void paintComponent ... class WinningsPanel extends JPanel implements ItemListener ...
    (comp.lang.java.gui)