Starting from this article we'll look at the new API for threading that we added after Java 5, and first look at the API documentation:
Java.util.concurrent contains many thread-safe, well tested, high-performance concurrent building blocks, let's look at the Atomicinteger under the atomic package first.
Import Java.util.concurrent.atomic.AtomicInteger;
public class Atomicintegertest {
private static atomicinteger data = new Atomicinteger (0);
public static void Main (string[] args {
new Thread () {new Runnable () {
@Override public
void Run () {
data.i Ncrementandget (); Plus
}
}). Start ();
New Thread (New Runnable () {
@Override public
void Run () {
data.decrementandget (); Minus
}
}). Start ();
}
}
Using Atomicinteger can be a convenient implementation of data sharing between threads, and if a member variable is to be manipulated by multiple threads, it can be handled using Atomicinteger, and other data types have corresponding atomic.