Introduction to Using the Raw Native Interface
 
 

Java & Native Code    PreviousRNINext
Introduction to Using the Raw Native Interface     Previous RNI Next

 


Synchronization

Native methods that are synchronized don't automatically lock\unlock the object. As a result, you must do any necessary synchronization on the native side.

  • ObjectMonitorEnter takes an object monitor
  • ObjectMonitorExit leaves an object monitor
  • ObjectMonitorNotify is analogous to Object.notify()
  • ObjectMonitorNotifyAll is analogous to Object.notifyAll()

For example, a typical synchronized method will look like the following:

    long cdecl Some_Java_SynchronizedMethod(struct HSomeJava *phThisUnsafe)
    {
        // Normal GC protection stuff.
        HSomeJava *phThisSafe;
        GCFrame gcf;
        
        GCFramePush(&gcf, &phThisSafe, sizeof(phThisSafe));
        phThisSafe = phThisUnsafe;
        
        ObjectMonitorEnter(phThisSafe);

        // Code here executes synchronized on this object.

        ObjectMonitorExit(phThisSafe);

        GCFramePop(&gcf);
   }

Top © 1998 Microsoft Corporation. All rights reserved. Terms of use.