Name: krC82822 Date: 05/07/2001
7 May 2001, kevin.ryan@eng -- see also # 4223287
java version "1.3.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0_02)
Java HotSpot(TM) Client VM (build 1.3.0_02, mixed mode)
It would be nice to provide access to an atomic test-and-set operation in Java.
The synchronized construct is sufficient for correctness, but is too
heavyweight for many operations. test-and-set or compare-and-swap would be
much more efficient for many operations.
One way to accomplish this would be to provide a class similar to C#
System.Threading.Interlocked. This might look something like this:
package java.lang;
import java.lang.reflect.Field;
public final class Atomic {
// Class exists only for its static methods.
private Atomic() {}
// Compare the value of Field on Object to the first value and if they are
// equal replace it with the second value. Return the original value
// of Field.
public static int compareAndExchange(Object, Field, int, int)
public static float compareAndExchange(Object, Field, float, float)
public static Object compareAndExcange(Object, Field, Object, Object)
// Replace the value of Field on Object with the value and return original
// value of Field
public static int exchange(Object, Field, int);
public static float exchange(Object, Field, float);
public static Object exchange(Object, Field, Object);
// Increment an integer Field on Object by one and return the new value.
public static int increment(Object, Field);
// Decrement an integer Field on Object by one and return the new value.
public static int decrement(Object, Field);
}
The biggest drawback I see to this sort of implementation is that it requires
reflection be used and I'm not sure that the VMs will be able to optimize these
operations efficiently. On the other hand it doesn't require changes to the
language. The other alternatives I can think of involve changing the syntax of
the language and perhaps even adding byte codes.
Rather than go on about other alternatives I'll just say that I hope that the
language design folks, in conjunction with the VM implementation folks, can
come up with a design which gives Java programmers access to a lighterweight
mechanism for doing test and replacement of references and increments and
decrements of numbers.
(Review ID: 123918)
======================================================================
Name: ca25432 Date: 01/22/2002
Name: krC82822 Date: 05/07/2001
7 May 2001, kevin.ryan@eng -- see also # 4223287
java version "1.3.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0_02)
Java HotSpot(TM) Client VM (build 1.3.0_02, mixed mode)
It would be nice to provide access to an atomic test-and-set operation in Java.
The synchronized construct is sufficient for correctness, but is too
heavyweight for many operations. test-and-set or compare-and-swap would be
much more efficient for many operations.
One way to accomplish this would be to provide a class similar to C#
System.Threading.Interlocked. This might look something like this:
package java.lang;
import java.lang.reflect.Field;
public final class Atomic {
// Class exists only for its static methods.
private Atomic() {}
// Compare the value of Field on Object to the first value and if they are
// equal replace it with the second value. Return the original value
// of Field.
public static int compareAndExchange(Object, Field, int, int)
public static float compareAndExchange(Object, Field, float, float)
public static Object compareAndExcange(Object, Field, Object, Object)
// Replace the value of Field on Object with the value and return original
// value of Field
public static int exchange(Object, Field, int);
public static float exchange(Object, Field, float);
public static Object exchange(Object, Field, Object);
// Increment an integer Field on Object by one and return the new value.
public static int increment(Object, Field);
// Decrement an integer Field on Object by one and return the new value.
public static int decrement(Object, Field);
}
The biggest drawback I see to this sort of implementation is that it requires
reflection be used and I'm not sure that the VMs will be able to optimize these
operations efficiently. On the other hand it doesn't require changes to the
language. The other alternatives I can think of involve changing the syntax of
the language and perhaps even adding byte codes.
Rather than go on about other alternatives I'll just say that I hope that the
language design folks, in conjunction with the VM implementation folks, can
come up with a design which gives Java programmers access to a lighterweight
mechanism for doing test and replacement of references and increments and
decrements of numbers.
(Review ID: 123918)
======================================================================
======================================================================