2015-05-12 100 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()) { 
} 

有沒有更高效,強大的方式來等待結果?

回答