Your semaphore allows ZERO passes through, in which case you have to
release first.

Either change the parameter to Semaphore constructor or release before
calling Semaphore.acquire()


Daniel


On 18 January 2012 15:00, tatebn <[email protected]> wrote:
> By the way, this code is located in a subclassed WebView.
>
>
> On Jan 18, 9:53 am, tatebn <[email protected]> wrote:
>> I'm trying to evaluate javascript with a semaphore lock.  I need to
>> query some web content about which context menus to show and need it
>> to be synchronous.  When I try to acquire the semaphore the thread
>> locks without the javascript being evaluated and my javascript
>> interface method being called.
>>
>> Here's my code.
>>
>> private Semaphore returnSemaphore = new Semaphore(0);
>>         private String returnValue;
>>
>>         public String getJSValue(String expression)
>>         {
>>                 String code = 
>> "javascript:_JSEval.setValue((function(){try{return "
>> + expression
>>                         + "+\"\";}catch(js_eval_err){return '';}})());";
>>                 //String code = "javascript: alert('here');";
>>                 Log.d(TAG, "eval: " + code);
>>
>>                 this.loadUrl(code);
>>
>>                 try {
>>                         Log.d(TAG, "eval: about to acquire semaphore, " +
>> returnSemaphore.availablePermits());
>>
>>                         returnSemaphore.acquire();
>>
>>                         Log.d(TAG, "past semaphore acquire");
>>
>>                         return returnValue;
>>                 } catch (InterruptedException e) {
>>                         Log.e(TAG, "eval: Interrupted", e);
>>                 }
>>                 return null;
>>
>>         }
>>
>>         @SuppressWarnings("unused")
>>         private class JSEval
>>         {
>>                 public void setValue(String value)
>>                 {
>>                         Log.i(TAG, "eval: in set value");
>>                         //if (value != null) {
>>                                 returnValue = value;
>>                         //}
>>                         returnSemaphore.release();
>>                 }
>>         }
>>
>> If I remove the semaphore acquire call, I get a result in setValue, so
>> I know the interface is working.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en



-- 
Daniel Drozdzewski

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to