在Java中等待的最佳方式 [英] Best way to wait in Java

查看:109
本文介绍了在Java中等待的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要等待一段时间的应用程序。它必须等到服务器填充了几个数据字段。

I have an app that needs to wait for some unknown amount of time. It must wait until several data fields are finished being populated by a server.

服务器的API为我提供了一种方法来请求数据,这很容易......

The server's API provides me a way to request data, easy enough...

服务器的API还提供了一种接收我的数据的方法,一次一个字段。它没有告诉我何时完成所有字段的填充。

The server's API also provides a way to receive my data back, one field at a time. It does not tell me when all of the fields are finished being populated.

等待服务器处理完请求后,最有效的方法是什么?这是一些伪代码:

What is the most efficient way to wait until my request is finished being processed by the server? Here's some pseudocode:

public class ServerRequestMethods {
    public void requestData();
}

public interface ServerDeliveryMethods {
    public void receiveData(String field, int value);
}

public class MyApp extends ServerRequestMethods implements ServerDeliveryMethods {
    //store data fields and their respective values
    public Hashtable<String, Integer> myData;    

    //implement required ServerDeliveryMethods
    public void receiveData(String field, int value) {
        myData.put(field, value);    
    }

    public static void main(String[] args) {
        this.requestData();

        // Now I have to wait for all of the fields to be populated,
        // so that I can decide what to do next.

        decideWhatToDoNext();
        doIt();
    }
}

我必须等到服务器完成填充我的数据字段,服务器在请求完成时不会通知我。所以我必须继续检查我的请求是否已经完成处理。最有效的方法是什么?

I have to wait until the server is finished populating my data fields, and the server doesn't let me know when the request is complete. So I must keep checking whether or not my request has finished processing. What is the most efficient way to do this?

wait()和notify(),一个保护while循环的方法,检查我是否具有所有必需的值但是每次我被notify()唤醒?

wait() and notify(), with a method guarding the while loop that checks if I have all of the required values yet every time I'm woken up by notify()?

Observer和Observable,有一个方法,每当我的观察者检查我是否拥有所有必需的值.Update()被调用?

Observer and Observable, with a method that checks if I have the all the required values yet every time my Observer.Update() is called?

什么是最好的方法?谢谢。

What's the best approach? Thanks.

推荐答案

如果我理解正确,其他一些线程调用 receiveData MyApp 上填写数据。如果那是对的,那么你就是这样做的:

If I understood you right, some other thread calls receiveData on your MyApp to fill the data. If that's right, then here's how you do it:


  1. 你这样睡觉:

  1. You sleep like this:

do {
    this.wait(someSmallTime); //We are aquiring a monitor on "this" object, so it would require a notification. You should put some time (like 100msec maybe) to prevent very rare but still possible deadlock, when notification came before this.wait was called.
} while (!allFieldsAreFilled());


  • receiveData应该通知打电话给取消暂停 等待打电话给你。例如:

  • receiveData should make a notify call, to unpause that wait call of yours. For example like this:

    myData.put(field, value);   
    this.notify();
    


  • 这两个块都需要在上同步这个对象能够获取它的监视器(等待所需)。您需要将方法声明为synchronized,或将相应的块放在 synchronized(this){...} 块中。

  • Both blocks will need to be "synchronized" on this object to be able to aquire it's monitor (that's needed for wait). You need to either declare the methods as "synchronized", or put the respective blocks inside synchronized(this) {...} block.

    这篇关于在Java中等待的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆