Created
November 12, 2016 18:19
-
-
Save jsbattig/927a768118cb6d3097fb7d64d32693ae to your computer and use it in GitHub Desktop.
Java KeepAlive() function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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