[concurrency-interest] JNI signaling back to a thread/concurrentstructure?

Gregg Wonderly gergg at cox.net
Mon Jul 20 10:29:31 EDT 2015


I have just had a thread running in a loop from JVM side to JNI side and blocking in JNI on a mutex.  I then unblocked it via the mutex and it would return to the Java side to do the necessary work.  You can include details of that work in the return value of the Java to JNI call.

Gregg Wonderly

Sent from my iPhone

> On Jul 14, 2015, at 4:21 AM, David Holmes <davidcholmes at aapt.net.au> wrote:
> 
> Hi Andrew,
> 
> Andrew Lentvorski writes:
>> This is probably a really stupid question, but is there a way to signal
>> a java.util.concurrent structure from native code that would allow a JNI
>> function to effectively "wake" a sleeping/blocked Java thread?
>> 
>> The context is this.  I have a JNI audio callback trucking along in
>> OpenSL on Android.  That callback is running in some sort of system
>> thread with highly elevated priority.  A Java thread fills a buffer
>> which the audio callback empties.  When the circular buffer is full, the
>> Java thread which fills that buffer goes to sleep for a bit until the
>> buffer empties out some.
>> 
>> However, if something is going wrong and the circular audio buffer is
>> about to empty, I would like to do something that would wake up the Java
>> thread or at least queue it on a relatively soon timeslice.  Maybe it
>> can recover and maybe it can't, but without the ability to signal that,
>> the thread is going to stay asleep and the audio buffer will drain.
>> 
>> Now, I can just wake the thread up every so many milliseconds, but
>> that's kind of wasteful of power, CPU, etc. as the majority of the time
>> the thread is just going to wake up to see that the buffer is fine,
>> realize it has no work and go back to sleep.
>> 
>> Any suggestions?  Or am I just completely barking mad for wanting this?
> 
> Use JNI to invoke Thread.interrupt. That will unblock the sleep, or most
> other concurrency-based blocking mechanisms.
> 
> David
> 
>> Thanks,
>> -a
>> 
>> 
>> 
>> 
> 
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest



More information about the Concurrency-interest mailing list