JDK-4455423 : Add an atomic test-and-set operation
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2001-05-07
  • Updated: 2002-02-25
  • Resolved: 2002-02-25
Related Reports
Duplicate :  
Description

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)
======================================================================


======================================================================

Comments
WORK AROUND Name: krC82822 Date: 05/07/2001 One can of course write native code to do this. It has the problem of not being easily ported and not being efficient because of the JNI overhead. The otherword around is to just use synchronization. This is more than sufficient for correctness and that in fact is the problem. Performance will suffer. ======================================================================
11-06-2004

EVALUATION Not entirely unreasonable; on the other hand, it isn't clear that it is worthwhile. Needs more thought. gilad.bracha@eng 2001-05-08 This feature will be made available as part of JSR-166 ("Concurrency Utilities") ###@###.### 2002-02-11
08-05-2001