How do I modify an int atomically and thread-safely in Java?
Atomically increment, test & set, etc...?
How do I modify an int atomically and thread-safely in Java?
Atomically increment, test & set, etc...?
Thread safety can be achieved via synchronized functions. Wrap your int (or such data) in a class which provides the required functionalities via synchronized methods, e.g.
public class X
{
protected int x;
public synchronized void set( int value )
{
x = value;
}
}
You can also use classes from the java.util.concurrent.atomic package, e.g. AtomicInteger or AtomicIntegerArray