如何在vaadin中实施短暂停顿

发布于 2025-02-07 02:00:30 字数 1108 浏览 2 评论 0原文

我正在尝试制作一个分类算法可视化器,该算法可视化的整数显示为条形,并显示每个步骤对数组的更改。我正在使用Vaadin 23.1.0和Java 17。到目前为止,所有分类算法和其他UI都可以正常工作,但是我无法在步骤之间简要暂停。我已经尝试使用thread.sleep and timeunit.seconds.sleep,但是在这两种情况下,步骤之间都不会停下来。它只是在视图顶部有一个长的加载条,然后显示末端排序的数组。 代码:

public class MainView extends VerticalLayout{
//set up UI and display initial unsorted array
buttonBeginSort.addClickListener(e -> {
            bubbleSort();
        });
}
public void bubbleSort()
{
        for (int i = 0; i < arr.length-1; i++)
        {
            for (int j = 0; j < arr.length-i-1; j++)
            {
                if (arr[j] > arr[j+1])
                {
                    swapIndexes(j,j+1);
                }
            }
        }
}
public void swapIndexes(int a, int b) {
        //swap array elements
        //display new, modified array

        waitBetweenSteps();
}
public void waitBetweenSteps()
    {
        try
        {
            Thread.sleep(500);
        }catch (InterruptedException ex)
        {
            Thread.currentThread().interrupt();
        }
}

考虑到所有这些,我该如何在Vaadin Web应用程序中进行简短暂停?

I'm trying to make a sorting algorithm visualizer, which visually displays the integers as bars and shows changes made to the array at each step. I am using Vaadin 23.1.0 and Java 17. All the sorting algorithms and other UI work properly so far, but I haven't been able to get it to pause briefly between steps. I've tried using Thread.sleep and TimeUnit.SECONDS.Sleep, but in both cases, it doesn't pause between steps. It just has a long loading bar at the top of the view, then shows the end sorted array.
Code:

public class MainView extends VerticalLayout{
//set up UI and display initial unsorted array
buttonBeginSort.addClickListener(e -> {
            bubbleSort();
        });
}
public void bubbleSort()
{
        for (int i = 0; i < arr.length-1; i++)
        {
            for (int j = 0; j < arr.length-i-1; j++)
            {
                if (arr[j] > arr[j+1])
                {
                    swapIndexes(j,j+1);
                }
            }
        }
}
public void swapIndexes(int a, int b) {
        //swap array elements
        //display new, modified array

        waitBetweenSteps();
}
public void waitBetweenSteps()
    {
        try
        {
            Thread.sleep(500);
        }catch (InterruptedException ex)
        {
            Thread.currentThread().interrupt();
        }
}

With all this considered, how can I make a brief pause in a Vaadin web app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

碍人泪离人颜 2025-02-14 02:00:30

Vaadin应用程序随着许多其他Web应用程序的运行,同时在服务器和浏览器中运行。用户交互和UI更新基于HTTP请求。因此,您无法基于完全同步过程设计该应用程序。

如果需要暂停,则需要使用执行程序在背景线程中运行过程,还需要启用推送 vaadin的功能,并使用UI#访问来定期从背景线程更新视图。

Vaadin application as many other web applications are running in server and browser at the same time. User interaction and UI updates are based on HTTP requests. Hence you can't design the application based on fully synchronous processes.

If you need a pause, you need to run your process in a background thread using Executor, you need to enable also the Push functionality of Vaadin and use UI#access to periodically update the view from the background thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文