package cc.monitors;

import cc.Parameters;

public abstract class AbstractTemporalMonitor
	extends AbstractMonitor
	implements Runnable {

	protected boolean finished = false;
	private Thread thread = null;
	private long waitTime = 0;

	public AbstractTemporalMonitor() {
		setWaitTime(Parameters.getInterval() * 1000);
		start();
	}

	public void start() {
		if (thread == null) {
			thread = new Thread(this, "Temporal Monitor");
			thread.start();
		}
	}

	public void run() {
		while (!finished) {

			evaluate();

			try {
				Thread.sleep(waitTime);
			} catch (InterruptedException e) {
				//e.printStackTrace();
			}
		}
	}

	public void setWaitTime(long ms) {
		waitTime = ms;
	}

	public void terminate() {
		finished = true;
		this.thread.interrupt();
	}
}
