package linechart;
import java.util.Random;
import javax.swing.SwingUtilities;
/**
*
* @author Rakesh Menon
*/
public class CPUData implements Runnable {
private static final Random random = new Random();
private UpdateListener updateListener = null;
private Thread thread = null;
private boolean stop = false;
public CPUData(UpdateListener ul) {
thread = new Thread(this);
this.updateListener = ul;
}
public void start() {
thread.start();
}
public void stop() {
stop = true;
}
public void run() {
while(!stop) {
/**
* Perform some "complex" tasks!
*/
final int user = random.nextInt(99) + 1;
final int sys = random.nextInt(100 - user);
final int idle = Math.abs(100 - user - sys);
/**
* Update listeners via Event Dispatch Thread
*/
SwingUtilities.invokeLater(new Runnable() {
public void run() {
updateListener.updateCPU(user, sys, idle);
}
});
/**
* Wait before performing next set of "complex" tasks!
*/
try {
Thread.sleep(2000);
} catch (Exception e) {
}
}
}
}
|