List<Callable<Long>> tasks
üretmek ana iş parçacığı var ... animasyon üzerindedir; sonra iş göndermek için bir
ExecutorService
kullanın.
Kullanıcı, hesaplamayı durdurmak istiyorsa, bu ikincil parçacığı iptal etme imkânsızlığı problemidir. İşte kod:
public class Animation extends Task<Long> {
protected Long call() throws Exception {
long startTime = System.currentTimeMillis();
WritableImage[] frames = new WritableImage[frameNumber];
List<Callable<Long>> tasks = new ArrayList<>();
updateProgress(count.incrementAndGet(), maxCount);
if (isCancelled()) {
return System.currentTimeMillis() - startTime;;
}
for (int k = 0; k < frameNumber; k++) {
frames[k] = new WritableImage(
(int) start.getCartesianPlane().getWidth(),
(int) start.getCartesianPlane().getHeight());
CartesianFractal tmp = FractalFactory.bulidFractal(
selectedFractal, nextDataBox(k), colorPalette);
tmp.setOnFinish(t -> {
updateProgress(count.incrementAndGet(), maxCount);
return null;
});
tasks.add((Callable<Long>) tmp);
if (isCancelled()) {
return System.currentTimeMillis() - startTime;;
}
}
executor = Executors.newFixedThreadPool(4);
updateProgress(count.incrementAndGet(), maxCount);
if (isCancelled()) {
return System.currentTimeMillis() - startTime;
}
try {
result = executor.invokeAll(tasks);
}
catch (InterruptedException ex) {
System.err.println(ex.toString());
}
// Check if all tasks are finished
boolean finished = false;
while (!finished) {
finished = true;
// Check if it is all done
for (Future<Long> r : result) {
finished = finished && r.isDone(); // THE PROGRAM BLOCKS HERE
// Check if the task was cancelled
if (isCancelled()) {
// Cancell all task
tasks.stream().forEach((t) -> {
((CartesianFractal)t).myCancel();
});
// Turnoff the executor
executor.shutdown();
return System.currentTimeMillis() - startTime;
}
}
}
// Turnoff the executor
executor.shutdown();
updateProgress(count.incrementAndGet(), maxCount);
makeAnimation();
updateProgress(count.incrementAndGet(), maxCount);
return System.currentTimeMillis() - startTime;
}
}
Future.isDone()
Future.get()
gibi program blok Gerçekten neden anlaşılmış yok!
Bu benim ilk sorum, umarım her şey yolundadır.
bunu mu:
programı
sadece meşgul görünüyor, bu yüzden bu çözüm bulduk:Animation
sınıf haline ederken: ben bu kodu kullanmak için tüm işleri iptal gerçekten * bloğu (iş parçacığı 'BLOCKED' durumunda olduğu gibi), ya da meşgul beklediği için bu satırda durmuş gibi görünüyor mu? –
Bu arada, bir iş parçacığı tamamlandığında bir "Tamamlama Hizmeti" bildiriminin daha kolay bir yolu olabilir. –
Başka bir BTW: 'System.currentTimeMillis() - startTime 'i döndürmek yerine, kodunuzla dağınık olarak, mantığın çoğunu' call() {long startTime =' den çağrılan vo void callInternal() 'içine bölebilirsiniz. ...; callInternal(); System.currentTimeMillis() - startTime döndürün; } '. Ayrıca, executor.shutDown() işlevini tek bir 'finally' bloğuna yerleştirmeyi düşünün. –