Sie tun es genau so, wie Sie es getan haben - es muss einen anderen Fehler in Ihrem Code geben.
Hier ist ein vollständiges, funktionierendes Beispiel, das das gleiche tut, was Sie tun. Die Ausgabe unten zeigt, dass es wie vorgesehen funktioniert.
public class Reschedule {
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
// Schedule three tasks
ScheduledFuture future1 = executor.scheduleAtFixedRate(() -> {
System.out.printf("%03ds: This is the first runnable, reporting in%n", (System.currentTimeMillis() - start)/1000);
}, 0, 5, TimeUnit.SECONDS);
ScheduledFuture future2 = executor.scheduleAtFixedRate(() -> {
System.out.printf("%03ds: This is the second runnable, reporting in%n", (System.currentTimeMillis() - start)/1000);
}, 2, 10, TimeUnit.SECONDS);
ScheduledFuture future3 = executor.scheduleAtFixedRate(() -> {
System.out.printf("%03ds: This is the third runnable, reporting in%n", (System.currentTimeMillis() - start)/1000);
}, 5, 15, TimeUnit.SECONDS);
// Wait some
Thread.sleep(30000);
// Reschedule the second task
System.out.printf("%03ds: Rescheduling the second runnable to run at 20 second intervals%n", (System.currentTimeMillis() - start)/1000);
future2.cancel(true);
future2 = executor.scheduleAtFixedRate(() -> {
System.out.printf("%03ds: This is the second runnable, reporting in%n", (System.currentTimeMillis() - start)/1000);
}, 2, 20, TimeUnit.SECONDS);
}
}
Wie Sie in der folgenden Ausgabe sehen die zweite Aufgabe setzt er Umschuldung und ändert es alle 10 Sekunden alle 20 Sekunden und die erste und dritte Aufgaben sind nicht betroffen vom Laufen.
Ausgang:
000s: This is the first runnable, reporting in
002s: This is the second runnable, reporting in
005s: This is the first runnable, reporting in
005s: This is the third runnable, reporting in
010s: This is the first runnable, reporting in
012s: This is the second runnable, reporting in
015s: This is the first runnable, reporting in
020s: This is the first runnable, reporting in
020s: This is the third runnable, reporting in
022s: This is the second runnable, reporting in
025s: This is the first runnable, reporting in
030s: This is the first runnable, reporting in
030s: Rescheduling the second runnable to run at 20 second intervals
032s: This is the second runnable, reporting in
035s: This is the first runnable, reporting in
035s: This is the third runnable, reporting in
040s: This is the first runnable, reporting in
045s: This is the first runnable, reporting in
050s: This is the first runnable, reporting in
050s: This is the third runnable, reporting in
052s: This is the second runnable, reporting in
055s: This is the first runnable, reporting in
060s: This is the first runnable, reporting in
065s: This is the first runnable, reporting in
065s: This is the third runnable, reporting in
070s: This is the first runnable, reporting in
072s: This is the second runnable, reporting in
Sie scheinen verwirrend Aufgaben mit Themen zu sein. Sie müssen * nicht * so viele Threads in Ihrem Thread-Pool haben, wie Sie Aufgaben haben, es sei denn, Ihre Aufgaben sind wirklich, wirklich schwer, Sie sollten mit einem Thread in Ordnung sein. Woher wissen Sie außerdem, dass im Executor nur eine Aufgabe geplant ist? – Raniz
Ich kann es in meinem Logfile sehen. Ok, ich denke du hast Recht. Ich verwechsle Aufgaben mit Threads. Also Executors.newScheduledThreadPool (1) funktioniert auch. Aber ist es möglich, die Verzögerung einer Aufgabe zu ändern? – pml