java - Set off a scheduled event at a given time? -
i'm writing java-based shell-type... erm.. thing. anyway, 1 of commands implementing schedule
command, can tell time want run command , waits time, runs command once, , exits. however, can't figure out how check time - implemented 1 checks if dates equal, did little research see if better solution existed, found apparently date
not thing use. should do?
mcve:
import java.util.date; public class scheduler extends thread { private date goat; private string command; public scheduler(date goat, string command) { this.goat = goat; this.command = command; } public void start() { super.start(); } public void run() { while (!goat.equals(new date())) {} //wait until dates equal runcommand(command); //in real thing, runs command. } public void runcommand(string command) { system.out.println("command: " + command); } public static void main(string[] args) { scheduler task = new scheduler(new date(system.currenttimemillis() + 5000)); task.start(); } }
i done without using third-party libraries, or libraries must downloaded separately. if there no way so, accept answers third-party library, preference goes solutions without them. ideal if answers allowed me directly specify time run command, rather calculating relative time difference , using that.
one option use timers/timertasks schedule commands - here tutorial.
another option put commands in delayqueue (with delay set time task should fire), use consumer thread repeatedly call take
on queue , fire commands removes.
in terms of third party libraries, quartz scheduler.
Comments
Post a Comment