5

Aggiungo tre thread con attività diverse e ritardi diversi al mio threadpool e tutto funziona perfettamente.Ritarda la modifica di una discussione nel threadpool

static ScheduledExecutorService scheduleTaskExecutor; 
static ScheduledFuture<?> future1; 
static ScheduledFuture<?> future2; 
static ScheduledFuture<?> future3; 

public void onCreate(Bundle savedInstanceState) { 
    scheduleTaskExecutor = Executors.newScheduledThreadPool(3); 
    future1 = scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable1() {...}, 0, olddelay1, TimeUnit.SECONDS); // Task 1 
    future2 = scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable2() {...}, 0, olddelay2, TimeUnit.SECONDS); // Task 2 
    future3 = scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable3() {...}, 0, olddelay3, TimeUnit.SECONDS); // Task 3 
} 

Più tardi durante il runtime voglio cambiare il ritardo di uno di questi fili (non importa quale), gli altri possono lavorare a bordo con il vecchio ritardo. Ho pensato che i riferimenti ScheduledFuture possano aiutare e provato il seguente codice (ad esempio per il secondo thread), ma dopo l'esecuzione è rimasto solo un thread nel threadpool (Task 2).

public void changeDelay2(int newdelay2){ 
    future2.cancel(true); 
    future2 = scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable2() {...}, 0, newdelay2, TimeUnit.SECONDS); // Task 2 
} 

Quindi esiste la possibilità di modificare il ritardo di un solo thread?

+0

Lei sembra essere compiti confondere con i filetti. Non * devi * avere tanti thread nel tuo pool di thread quando hai delle attività, a meno che le tue attività siano davvero molto pesanti e dovresti stare bene con un thread. Inoltre, come fai a sapere che ci sono solo 1 compito in programma nell'esecutore? – Raniz

+0

Posso vederlo nel mio file di registro. Ok, penso che tu abbia ragione. Sto confondendo i compiti con i thread. Quindi Executors.newScheduledThreadPool (1) funziona altrettanto bene. Ma è possibile modificare il ritardo di un compito? – pml

risposta

2

Lo fai esattamente come hai fatto - ci devono essere altri errori nel codice.

Ecco un esempio completo e funzionante che fa la stessa cosa che stai facendo. L'output sotto mostra che funziona come previsto.

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); 
    } 
} 

Come si può vedere nell'output sotto riprogrammazione secondo compito azzera e cambia da ogni 10 secondi per ogni 20 secondi e il primo e il terzo compito sono influenzate.

uscita:

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 
+0

grazie, il tuo esempio funziona bene! ci deve essere qualche altro errore nel mio codice .. – pml

Problemi correlati