Category: Proxy usage

Q

Can I explicitly use Java synchronization from .NET?

A

In Java, you can use the synchronized keyword to create a critical section in which one thread has exclusive access to a Java object. In JNI, you can use the MonitorEnter() and MonitorExit() functions for the same purpose. While we can't give you the synchronized keyword in .NET, we can do something that is almost as good.

The runtime library provides you with the JavaProxyMonitor and JavaProxyLockHolder utility types for the purpose of providing synchronization on a Java object. Here's how you might use them in your code:

MyProxyType       mpt = MyProxyType.GetInstance();
// this is the scope to which the synchronization applies using( JavaProxyLockHolder plh = new JavaProxyLockHolder( mpt ) ) { ... } // the plh object is disposed of and
// its Dispose() method releases the lock

You can synchronize on a type, i.e. the static members of a Java class, by using the return value from a Class.ForName() call as the lock object:

Class          clz = Class.ForName( "com.mypackage.MyProxyType" );
// this is the scope to which the synchronization applies using( JavaProxyLockHolder plh = new JavaProxyLockHolder( clz ) ) { ... } // the plh object is disposed of and
// its Dispose() method releases the lock

 


Copyright 2006-2011 by Codemesh, Inc., ALL RIGHTS RESERVED

:
frequently asked questions
home products support customers partners newsroom about us contact us