I'm trying to monitor a group of computer resources online. In order to do that, i'm using JFreechart and try to use multithreading in order to gain performance.
Here is my code:
class osGenel extends JPanel implements Runnable {
String name;
private static DefaultValueDataset[] DATASET;
Integer VALUE,iii,sayac;
Thread t;
private static Sessionn[] obj;
osGenel(String s,DefaultValueDataset valuedataset,Double n,Double w,Double c,int i) {
name = s;
iii=i;
t = new Thread(this, name);
this.setLayout(new GridLayout());
DATASET[iii]=valuedataset;
MeterPlot meterplot = new MeterPlot(DATASET[iii]);
meterplot.setDialShape(DialShape.CIRCLE);
meterplot.setRange(new Range(0.0D, 100D));
meterplot.addInterval(new MeterInterval("Normal", new Range(0.0D, n), Color.lightGray, new BasicStroke(2.0F),Color.GREEN));
meterplot.addInterval(new MeterInterval("Warning", new Range(n, w), Color.lightGray, new BasicStroke(2.0F), Color.YELLOW));
meterplot.addInterval(new MeterInterval("Critical", new Range(w, c), Color.lightGray, new BasicStroke(2.0F), Color.RED));
meterplot.setNeedlePaint(Color.darkGray);
meterplot.setDialBackgroundPaint(Color.white);
meterplot.setDialOutlinePaint(Color.gray);
meterplot.setMeterAngle(260);
meterplot.setTickLabelsVisible(true);
meterplot.setTickLabelFont(new Font("Dialog", 1,25));
meterplot.setTickLabelPaint(Color.darkGray);
meterplot.setTickSize(5D);
meterplot.setBackgroundPaint(Color.black);
meterplot.setTickPaint(Color.lightGray);
meterplot.setValuePaint(Color.black);
meterplot.setValueFont(new Font("Dialog", 1, 0));
JFreeChart chart = new JFreeChart(s, JFreeChart.DEFAULT_TITLE_FONT, meterplot, true);
this.add(new ChartPanel(chart, false));
t.start();
}
public void run() {
try {
for (int i = 100; i > 0; i--) {
System.out.println(iii + ". Thread running");
Random r=new Random();
DATASET[iii].setValue(r.nextInt(100));
Thread.sleep(3000);
}
} catch (InterruptedException e) {
System.out.println(name + "Interrupted");
}
System.out.println(name + " Out.");
}
public static void main(String args[])
{
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
DefaultValueDataset defaultvaluedataset = new DefaultValueDataset(23D);
final JFrame f = new JFrame("OS MONITOR");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
obj=new Sessionn[9];
DATASET=new DefaultValueDataset[9];
f.setLayout(new GridLayout(3, 3));
f.add(new osGenel("CPU", defaultvaluedataset,60D,90D,100D,0));
f.add(new osGenel("DISK", defaultvaluedataset,60D,90D,100D,1));
f.add(new osGenel("MEMORY", defaultvaluedataset,60D,90D,100D,2));
f.add(new osGenel("CPU", defaultvaluedataset,60D,90D,100D,3));
f.add(new osGenel("DISK", defaultvaluedataset,60D,90D,100D,4));
f.add(new osGenel("MEMORY", defaultvaluedataset,60D,90D,100D,5));
f.add(new osGenel("CPU", defaultvaluedataset,60D,90D,100D,6));
f.add(new osGenel("DISK", defaultvaluedataset,60D,90D,100D,7));
f.add(new osGenel("MEMORY", defaultvaluedataset,60D,90D,100D,8));
f.pack();
f.setVisible(true);
}
});
}
}
The problem is, when i run this program, my threads are working, in each thread there is different value, but my datasets always get same value from these threads. I thought my each dataset will get different value, but it doesn't. All graphs show same value with each thread works.
You have one big problem, you use one instance of DefaultValueDataset
for all your threads, in next code :
DefaultValueDataset defaultvaluedataset = new DefaultValueDataset(23D);
f.add(new osGenel("CPU", defaultvaluedataset,60D,90D,100D,0));
f.add(new osGenel("DISK", defaultvaluedataset,60D,90D,100D,1));
f.add(new osGenel("MEMORY", defaultvaluedataset,60D,90D,100D,2));
f.add(new osGenel("CPU", defaultvaluedataset,60D,90D,100D,3));
f.add(new osGenel("DISK", defaultvaluedataset,60D,90D,100D,4));
f.add(new osGenel("MEMORY", defaultvaluedataset,60D,90D,100D,5));
f.add(new osGenel("CPU", defaultvaluedataset,60D,90D,100D,6));
f.add(new osGenel("DISK", defaultvaluedataset,60D,90D,100D,7));
f.add(new osGenel("MEMORY", defaultvaluedataset,60D,90D,100D,8));
Because of that, you have the same result in each panel.
Change that code to:
f.add(new Test("CPU", new DefaultValueDataset(23D), 60D, 90D, 100D, 0));
f.add(new Test("DISK", new DefaultValueDataset(23D), 60D, 90D, 100D, 1));
f.add(new Test("MEMORY", new DefaultValueDataset(23D), 60D, 90D, 100D, 2));
f.add(new Test("CPU", new DefaultValueDataset(23D), 60D, 90D, 100D, 3));
f.add(new Test("DISK", new DefaultValueDataset(23D), 60D, 90D, 100D, 4));
f.add(new Test("MEMORY", new DefaultValueDataset(23D), 60D, 90D, 100D, 5));
f.add(new Test("CPU", new DefaultValueDataset(23D), 60D, 90D, 100D, 6));
f.add(new Test("DISK", new DefaultValueDataset(23D), 60D, 90D, 100D, 7));
f.add(new Test("MEMORY", new DefaultValueDataset(23D), 60D, 90D, 100D, 8));
And you see expected result.