I'm facing a tough problem. I am using JFreechart in Eclipse RCP to write a system resource monitor. I implemented a class DatGenerator as follow:
Code: Select all
public class DataGenerator extends Timer implements ActionListener{
private static final long serialVersionUID = 3977867288743720505L;
public DataGenerator(int interval){
super(interval,null);
System.out.println("in DataGenerator constructor");
addActionListener(this);
}
public void actionPerformed(ActionEvent event){
long f=Runtime.getRuntime().freeMemory();
long t=Runtime.getRuntime().totalMemory();
System.out.println("freeMem: " + f + "\t\tTotalMem: " + t);
MemChart.addTotalObservation(t);
MemChart.addFreeObservation(f);
}
}
Code: Select all
public class MemChart {
static JFreeChart chart;
static final int HISTORY_COUNT = 30000;
static TimeSeriesCollection dataset;
static TimeSeries total = new TimeSeries("TotalMemory",Millisecond.class);
static TimeSeries free = new TimeSeries("FreeMemory",Millisecond.class);
public static void createDataset(){
free.setMaximumItemCount(HISTORY_COUNT);
total.setMaximumItemCount(HISTORY_COUNT);
dataset = new TimeSeriesCollection();
dataset.addSeries(total);
dataset.addSeries(free);
}
public static JFreeChart createChart(){
createDataset();
DateAxis domain = new DateAxis("Time");
NumberAxis range = new NumberAxis("Memory");
domain.setTickLabelFont(new Font("SansSerif", Font.PLAIN,12));
range.setTickLabelFont(new Font("SansSerif",Font.PLAIN,12));
domain.setLabelFont(new Font("SansSerif",Font.PLAIN,14));
range.setLabelFont(new Font("SansSerif",Font.PLAIN,14));
XYItemRenderer renderer = new XYLineAndShapeRenderer(true,false);
renderer.setSeriesPaint(0,Color.red);
renderer.setSeriesPaint(1,Color.green);
renderer.setStroke(new BasicStroke(3f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL)
);
XYPlot plot = new XYPlot(dataset,domain,range,renderer);
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setAxisOffset(new RectangleInsets(5.0,5.0,5.0,5.0));
domain.setAutoRange(true);
domain.setLowerMargin(0.0);
domain.setUpperMargin(0.0);
domain.setTickLabelsVisible(true);
range.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
chart = new JFreeChart(
"JVM Memory Usage",
new Font("SansSerif", Font.BOLD, 24),
plot,
true
);
chart.setBackgroundPaint(Color.white);
return chart;
}
public static void addTotalObservation(final double y){
System.out.println("in addTotalObservation");
total.add(new Millisecond(),y);
}
public static void addFreeObservation(final double y){
System.out.println("in addFreeObservation");
free.add(new Millisecond(),y);
}
}
so i tried to add this:
Code: Select all
Display.getCurrent().syncExec(new Runnable(){
public void run(){
free.add(new Millisecond(),y);
}
});