Hello everybody,

I have a task to let an engine run. Now I want it to program that it runs once for 60 seconds and then stops. How can I do this? Timer is not an option because it don't need to repeat. Thanks in advance for your answers :)

Greetz

MeandJava

Recommended Answers

All 8 Replies

Timer is an option. Timers can be one-shot OR repeating. You need a timer with a 60 second initial delay and no repeats.

I'm sorry I only used a Timer for doing tasks multiple times. I didn't knew you could use the timer for tasks to run once for ... seconds. Could you give me an example? I tried to change period to null, but he doesn't take that. Thanks in advance :) By the way the code I have is placed in a method:


public void startEngine1 (int speed) {

}

Greetz MeandJava

Its all in the API JavaDoc, depending on which Timer class you are using.

OK, I have a little more time now...
You have a choice of java.util.Timer and javax.swing.Timer.
The first is a general purpose Timer, the second is tied into the Swing GUI environment, and should be used for any kind of GUI timing (animation etc).
For java.util.Timer you can use

public void schedule(TimerTask task, long delay)

Schedules the specified task for execution after the specified delay.

Parameters:
task - task to be scheduled.
delay - delay in milliseconds before task is to be executed.

For javax,swing.Timer it's more like

public Timer(int delay, ActionListener listener)

Creates a Timer and initializes both the initial delay and between-event delay to delay milliseconds. If delay is less than or equal to zero, the timer fires as soon as it is started. If listener is not null, it's registered as an action listener on the timer.

Parameters:
delay - milliseconds for the initial and between-event delay
listener - an initial listener; can be null

plus

public void setRepeats(boolean flag)

If flag is false, instructs the Timer to send only one action event to its listeners.

Thanks for the information you posted. This is what i came up with. The only problem is that it waits 6 seconds and then it starts to run and never stops. How can I get it to run for 60 sec and then stop. Thanks in advance. I really appreciate the time you are putting in to help me.

public boolean startMotor1(final int p) {
        
    this.speed = p; //speed of the engine
    long delay = 6000;   // delay for 6 sec.
    Timer timer = new Timer();

    timer.schedule(new TimerTask() {
        public void run() {
            // Task here ...
            try {
                if (dev.isControllerAlive()) { //dev is the hardware device
                    bot.motor1_forward(); //motor goes forward
                    bot.motor1_speed(p); //Speed of the motor
                }
            } catch (Exception e) {
            System.out.println("ERROR, device missing");

            }
        }
    }, delay);

        return dev.isControllerAlive();
    }

Almost right...
correct sequence of events is:

start motor
start timer (60000 mSec, not 6000 for 60 secs)
in TimerTask: stop motor

Thank you very much :) It works now. I will mark this thread as solved.

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.