Hello.
Currently i am creating a small application that works around packets.
The packets are sent every 600ms.

Now my quesition is this:
Currently i have this (An example)

public class Example	{
	private int exampleInt = 0;
	
	public void setExample(int i)	{
		exampleInt = i;
	}
	
	public void getExample()	{
		return exampleInt;
	}	
}

Now here is my other example class

public class OtherExample	{

	private Example e = new Example();
	
	public void someAction()	{
		if(e.getExample == 0)	{
			e.setExample(30);
		} else {
			System.err.println("Please wait before doing this action again");
		}
	}

	/* Is refreshed every 600ms */
	public void process()	{
		if(e.getExample() > 0)	{
			e.getExample() -= 1;
		}
	}
}

Whats happens is every 600ms the system checks to see if the integer is great than 0. if so its subtracts 0 from the total until it is 0 again. So if i wanted 1 second i would set the integer to 1 and a bit :)

Now as you can see this is a poor way of doing a timer. But what i want to achieve is when a certain packet is recieved a timer system is in work to wait until the timer has run out before the packet can be read again.

Please help because as you can see i have a very poor way of doing it :)

Any suggestion would be helpful

public class OtherExample	{

	private Example e = new Example();
	
	public void someAction()	{
		if(e.getExample == 0){ // error, e.getExample not declared/found (forgot parenthesis)
			e.setExample(30);
		} else {
			System.err.println("Please wait before doing this action again");
		}
	}

	/* Is refreshed every 600ms */
	public void process()	{
		if(e.getExample() > 0)	{
			e.getExample() -= 1; // error,! methods cant be used as lvalues
		}
	}
}

Other than the errors listed above, you're simply trying to monitor time being elapsed then do something, correct? The class I'm going to provide may or may not be of use for you. Hopefully it will be. I call it Clock when really it's just a virtual stop-watch--

import java.util.concurrent.*;
import javax.swing.JOptionPane;

public class Clock{

	private ExecutorService es = Executors.newFixedThreadPool(1);
	private long startTime = 0, finishTime = 0, decided = 0;
	private boolean isStarted = false, wasReset = true;
	public static final long SECONDS = 1000000000, DECASECONDS = 100000000, CENTISECONDS = 10000000, MILLISECONDS = 1000000;

	public Clock(int timeType) throws Throwable{
		setTimeType(timeType);
	}

	public Clock(long timeType) throws Throwable{
		setTimeType(timeType);
	}

	public void setTimeType(int timeType) throws Throwable{
		if((!isStarted) && wasReset){
			switch(timeType){
				case 0: decided = SECONDS;
					break;
				case 1: decided = DECASECONDS;
					break;
				case 2: decided = CENTISECONDS;
					break;
				case 3: decided = MILLISECONDS;
					break;
				default:{
					this.finalize();
					throw new Exception("Improper timetype");
				}
			}
		}
		else JOptionPane.showMessageDialog(null, "Please reset the clock before using a different timetype.");
	}

	public void setTimeType(long timeType) throws Throwable{
		if((!isStarted) && wasReset){
			if(timeType == SECONDS)
				decided = SECONDS;
			else if(timeType == DECASECONDS)
				decided = DECASECONDS;
			else if(timeType == CENTISECONDS)
				decided = CENTISECONDS;
			else if(timeType == MILLISECONDS)
				decided = MILLISECONDS;
			else{
				this.finalize();
				throw new Exception("Improper timetype");
			}
		}
		else JOptionPane.showMessageDialog(null, "Please reset the clock before using a different timetype.");
	}

	Runnable rn = new Runnable(){
		public void run(){ while(isStarted){ finishTime = (System.nanoTime()/decided); } }
	};

	public synchronized void start(){
		if(!isStarted){
			wasReset = false;
			isStarted = true;
			startTime = (System.nanoTime()/decided);
			es.execute(rn);
		}else JOptionPane.showMessageDialog(null, "The clock has already started.");
	}

	public synchronized void stop(){
		if(isStarted){
			isStarted = false;
		}else JOptionPane.showMessageDialog(null, "The clock has already stopped.");
	}

	@Override
	protected void finalize() throws Throwable{
		try{
			isStarted = false;
		}finally{
			es.shutdown();
			super.finalize();
		}
	}

	public synchronized long getTimeElapsed(){
		return finishTime - startTime;
	}

	public synchronized long getCurrentStart(){
		return startTime;
	}

	public synchronized long getCurrentFinish(){
		return finishTime;
	}

	public synchronized void resetTimer(){
		if(!isStarted){
			wasReset = true;
			startTime = 0;
			finishTime = 0;
		}else JOptionPane.showMessageDialog(null, "Please stop the clock before trying to reset it.");
	}
}

Sorry about the errors. I never noticed them :)

And thank you. Thats what i really wanted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.