2015-05-12 99 views
1

我有cordova插件,它工作。然而,等待hashmap的while循环似乎没有效率。有没有更好的办法?Java如何检测变量变化

public class MyApp extends CordovaPlugin { 
    static HashMap<String,String> myProp = new HashMap<String,String>(); 

    protected void pluginInitialize() { 
    } 

    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) 
     throws JSONException { 
    if (action.equals("init")) { 
     AppConnectionService.requestConfig(cordova.getActivity()); 

     init(callbackContext); 

     PluginResult.Status status = PluginResult.Status.NO_RESULT; 
     PluginResult pluginResult = new PluginResult(status); 
     pluginResult.setKeepCallback(true); 
     callbackContext.sendPluginResult(pluginResult);  

     return true;  
    } 
    return false; 
    } 

    private void init(final CallbackContext callbackContext) { 
    while(myProp.isEmpty()) { 
    } 

    PluginResult result = new PluginResult(PluginResult.Status.OK, myProp.get("myID")); 
    result.setKeepCallback(false); 
    callbackContext.sendPluginResult(result);   
    } 

    public static boolean handleConfig (Bundle config) { 
     if (config != null) { 
     // Add all the entries that came in the config for the display adapter 
     for (String key: config.keySet()) {    
      myProp.put(key, config.getString(key)); 
     } 
     } else { 
      Log.d("MyApp", "Failed"); 
     } 
     return false; 
    } 
} 

所以在JavaScript的部分,它会调用init(),并返回“身份识别码”,但我在等待结果的方式是一个繁忙的循环:

while(myProp.isEmpty()) { 
} 

有没有更高效,强大的方式来等待结果?

回答