Skip to content

Instantly share code, notes, and snippets.

@jsbattig
Created November 12, 2016 18:19
Show Gist options
  • Save jsbattig/927a768118cb6d3097fb7d64d32693ae to your computer and use it in GitHub Desktop.
Save jsbattig/927a768118cb6d3097fb7d64d32693ae to your computer and use it in GitHub Desktop.
Java KeepAlive() function
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
public class NativeMember {
private static int staticCounter = 0;
private static final AtomicIntegerFieldUpdater<NativeMember> updater = AtomicIntegerFieldUpdater.newUpdater(NativeMember.class, "counter");
private volatile int counter = staticCounter;
public void keepAlive() {
updater.lazySet(this, counter + 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment