我正在使用scheduledexecutorservice来执行动画绘制图形(一次一个顶点和一个边)。我在逐步更新实际UI时遇到了问题,而我看不到实际的动画,但只能看到最终的图形。我应该怎么做才能防止UI冻结(scheduledexecutorservice)
private Runnable newRunnable() {
return new Runnable() {
@Override
public void run() {
// this method just adds the graph to a JPanel
displayDiagram();
}
};
}
private void animate() {
executorService.schedule(newRunnable(), 1500, TimeUnit.MILLISECONDS);
executorService.shutdown();
try {
executorService.awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
executorService = new ScheduledThreadPoolExecutor(1);
}
我想达到的目标是能够得出一个顶点和一个边缘,每一次更新UI,然后等待1500毫秒,直到显示整个图形再次做到这一点。
当动态创建图时,方法动画会被多次调用。
这种“_executorService.awaitTermination_”排队到Swing事件队列的后台线程中进行的任何摇摆电话究竟它在锡上说的 - 它**等待**。不要称这个。使用'SwingWorker'。 –