Using the Raw Native Interface Previous
Previous
Introduction
Introduction
Index
Index
Next
Next

Synchronization

Native methods marked as being 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 it, ObjectMonitorNotify() is analogous to Object.notify(), and 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);
   }


© 1997 Microsoft Corporation. All rights reserved. Legal Notices.