本文介绍了我应该怎么做,以防止UI冻结(scheduledexecutorservice)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用scheduledexecutorservice来执行绘制图形的动画(一次一个顶点和一个边)。我有一个步骤更新实际UI的问题,而我看不到实际的动画,但只有最后的图形。

I am using a scheduledexecutorservice to perform an animation for drawing a graph(one vertex and one edge at a time). I have a problem in updating the actual UI step by step and instead I can't see the actual animation but only the final graph.

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,然后等待1500ms并再次执行,直到显示整个图。

What I want to achieve is to be able to draw one vertex and one edge, and update the UI every time and then wait for 1500ms and do that again until the whole graph is displayed.

方法动画将被多次调用,因为图是动态的正在创建。

Method animate would be called multiple times as the graph is dynamically being created.

推荐答案

awaitTermination 是一种阻塞方法。相反,你应该使用 SwingWorker (并利用 publish / / code>功能)或Swing 计时器

awaitTermination is a blocking method. Instead you should either be using a SwingWorker (and taking advantage of it's publish/process functionality) or a Swing Timer.

查看,和了解更多详情

也许像

这篇关于我应该怎么做,以防止UI冻结(scheduledexecutorservice)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:18