5

olarak değiştirmem İş parçacığım için farklı görevler ve farklı gecikmeler içeren üç iş parçacığı ekliyorum ve her şey gayet iyi çalışıyor.Bir iş parçacığının geçme süresini

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 
} 

Daha sonra çalışma zamanı sırasında ben bu konuları birinde (olursa olsun) gecikmesini değiştirmek için, diğerleri ise eski gecikme ile çalışmak zorundadır istiyorum. ScheduledFuture referanslarının aşağıdaki kodu (örneğin ikinci iş parçacığı için) yardımcı olabileceğini ve denediğini düşündüm, ancak yürütme sonrasında threadpool'da yalnızca bir iş parçacığı kalmıştır (Görev 2).

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

Yani, yalnızca bir iş parçacığının gecikmesini değiştirme olasılığı var mı?

+0

. Görevleriniz gerçekten çok ağır olmuyorsa, iş parçacığınız olduğu gibi, iş parçacığı havuzunuzda çok fazla iş parçacığına sahip olmanız gerekir. Ek olarak, yürütücüde yalnızca 1 görev planlandığını nereden biliyorsunuz? – Raniz

+0

Bunu günlük dosyamda görebiliyorum. Tamam, bence haklısın. Konuları iş parçacıkları ile karıştırıyorum. Yani Executors.newScheduledThreadPool (1) de işe yarıyor. Ama bir görevin gecikmesini değiştirmek mümkün mü? – pml

cevap

2

Bunu yaptığınız gibi yaparsınız - kodunuzda başka bir hata olması gerekir.

İşte yaptığınız işi yapan eksiksiz, çalışan bir örnek. Aşağıdaki çıktı, amaçlandığı gibi çalıştığını göstermektedir.

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

ikinci görevi yeniden programlama aşağıdaki çıktıda görebileceğiniz gibi

sıfırlar ve her 20 saniyede her 10 saniyede bir fotoğraf ve birinci ve üçüncü görevler etkilenmeyen onu değiştirir.

Çıktı: Sen parçacığı ile kafa karıştırıcı görevler gibi görünüyor

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

Teşekkürler, örneğiniz iyi çalışıyor! Kodumda başka bir hata olmalı. – pml

İlgili konular