package dataBase;

public abstract class TimedAction extends Thread {
	
	private long ms;
	
	/**
	 * Constructor
	 * @param ms - time to wait before starting action (in thread)
	 */
	public TimedAction(long ms){
		this.ms = ms;
		this.start();
	}
	
	/** do not overwrite **/
	public void run(){
		
		if(ms<1){			// do it now!
			doAction();
			return;
		}
		
		try {
			sleep(ms);
		} catch (InterruptedException e) {
			System.err.println("Interrupted TimedAction: "+e);
		}
		
		doAction();
	}
	
	/**
	 * Catches exceptions from action function
	 */
	private void doAction(){
		try {
			action();
		} catch (Exception e) {
			System.err.println("Exception in TimedAction: " + e.getMessage());
		}
	}
	
	/**
	 * Abstract action function to implement.
	 * @throws Exception
	 */
	public abstract void action() throws Exception;
	
}
