Interacting with a Java monitor from native code

So far, we have been synchronizing access to shared resources in Java threads using synchronized statements or synchronized methods:

synchronized (obj) { ... // synchronized block }
synchronized void incrementCount() { ... // synchronized methods }

When we are executing a native method and want to have access to a resource or variable shared between multiple Java code and native code, the JNI offers us MonitorEnter and MonitorExit methods to control access to the mutual exclusion zone managed by a Java synchronized block:

jint MonitorEnter(JNIEnv *env, jobject obj);
jint MonitorExit(JNIEnv *env, jobject obj);

MonitorEnter, the function responsible for acquiring access to the Java monitor scope, might ...

Get Asynchronous Android Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.