Skip to content

Instantly share code, notes, and snippets.

@nitsanw
Created December 13, 2012 12:49
Show Gist options
  • Save nitsanw/4276162 to your computer and use it in GitHub Desktop.
Save nitsanw/4276162 to your computer and use it in GitHub Desktop.
VolatileLong
private volatile long value;
private static final long valueOffset;
static {
try {
valueOffset = UnsafeAccess.unsafe.objectFieldOffset(VolatileLong.class.getDeclaredField("value"));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void set(final long value) {
this.value = value;
}
public long get() {
return value;
}
public long directGet() {
return UnsafeAccess.unsafe.getLong(this, valueOffset);
}
public void directSet(final long value) {
UnsafeAccess.unsafe.putLong(this, valueOffset, value);
}
public void lazySet(final long value) {
UnsafeAccess.unsafe.putOrderedLong(this, valueOffset, value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment