RSS feed [root] /weblog /concurrency /java




login:

password:

title search:




 


Sun Aug 28 11:58:48 GMT 2011

monitoring



Sample code of try-sync


import sun.misc.*;

import java.lang.reflect.*;

public class MonitorUtils {
private static Unsafe unsafe = getUnsafe();

public static boolean trySynchronize(Object monitor) {
return unsafe.tryMonitorEnter(monitor);
}

public static void unsynchronize(Object monitor) {
unsafe.monitorExit(monitor);
}

private static Unsafe getUnsafe() {
try {
for (Field field : Unsafe.class.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers())) {
if (field.getType() == Unsafe.class) {
field.setAccessible(true);
return (Unsafe) field.get(null);
}
}
}
throw new IllegalStateException("Unsafe field not found");
} catch (Exception e) {
throw new IllegalStateException(
"Could not initialize unsafe", e);
}
}
}


http://www.javaspecialists.eu/archive/Issue194.html


(google search) (amazon search)
second
download zip of files only