を停止します。FUNC()が呼び出されたときScheduledThreadPoolExecutorのは、私は次のコードを書かれている方法を実行可能なクラスJAVA
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
}
}
私はScheduledThreadPoolExecutorのを停止したいと思います。それがさらに働く必要はありません!私は、関数func()をVoterクラスの中に入れて、何らかのコールバックを作成する必要があると思います。しかし、おそらく私はShootクラスの中からそれを行うことができます。
どうすれば適切に解決できますか?
私は 'ScheduledThreadPoolExecutor'についてよく知っているわけではありませんが、私の最初の推測は' stpe.shutdown() 'でしょう。 'stpe'を' func() 'から到達可能にする必要があります。デッドライン時に実行される第2のスレッド(スケジュール?)を開始し、 'shutdown()'関数を呼び出すことは合理的かもしれません。 – Dorus