I have written the following code:
import java.util.Calendar;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
class Voter {
public static void main(String[] args) {
ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(2);
stpe.scheduleAtFixedRate(new Shoot(), 0, 1, TimeUnit.SECONDS);
}
}
class Shoot implements Runnable {
Calendar deadline;
long endTime,currentTime;
public Shoot() {
deadline = Calendar.getInstance();
deadline.set(2011,6,21,12,18,00);
endTime = deadline.getTime().getTime();
}
public void work() {
currentTime = System.currentTimeMillis();
if (currentTime >= endTime) {
System.out.println("Got it!");
func();
}
}
public void run() {
work();
}
public void func() {
// function called when time matches
}
}
I would like to stop the ScheduledThreadPoolExecutor when the func() is called. There is no need for it to work futher! I think i should put the function func() inside Voter class, and than create some kind of callback. But maybe I can do it from within the Shoot class.
How can I solve it properly?