有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaGWT:如何创建我自己的异步方法?

有没有办法在GWT上创建自己的异步方法?我使用的是GWT2.7,似乎没有java并发类与gwt兼容

这是我的故事。我的客户端有一个服务类,它缓存一些服务器数据。它有一个普通的getter方法(同步)来获取缓存数据,还有一个reload方法来更新缓存。(当然,它是异步的)。发送重新加载请求后,应该禁用getter方法,直到更新完成

现在,每次使用getter方法时,我都会将其包装在计时器中。这很好,但我想知道有没有更好的方法。这是太多的样板代码

    final AutoProgressMessageBox messageBox = 
            ServiceManager.createProgressMessage("Progress", "Loading Products...");
    Timer timer = new Timer(){
        @Override
        public void run() {
            if(!serviceManager.isProductLocked()){
                // already loaded
                serviceManager.getProducts();
                // do my work...
                messageBox.hide();
                this.cancel();
            }
        }
    };
    timer.scheduleRepeating(2000);

共 (1) 个答案

  1. # 1 楼答案

    GWT有^{}类,它允许您访问一些模拟多线程环境的静态方法。具体来说,您可能对^{}方法感兴趣:

    Schedules a repeating command that is scheduled with a constant delay. That is, the next invocation of the command will be scheduled for delayMs milliseconds after the last invocation completes.