AtomicInteger class” understanding and use
First look at the two paragraph of code, one is Integer, and the other is AtomicInteger.
public class Sample1 { private static Integer count = 0; synchronized public static void increment() { count++; } }
The following is AtomicInteger’s:
public class Sample2 { private static AtomicInteger count = new AtomicInteger(0); public static void increment() { count.getAndIncrement(); } }
When using Integer, you must add synchronized to ensure that no concurrent threads are accessed at the same time, but not in Atomic Integer, where AtomicInTeger provides atomic operations. Here is a brief introduction.
AtomicInteger introduction”
AtomicIntegerIt is a Integer class that provides atomic operations, which is operated by thread safety.
AtomicInteger usage scenario”
AtomicIntegerAtomic operations are provided for the use of Integer, so it is highly suitable for use in high concurrency situations.
AtomicInteger source part of the explanation”
public class AtomicInteger extends Number implements java.io.Serializable { private static final long serialVersionUID = 6214790243416807050L; // setup to use Unsafe.compareAndSwapInt for updates private static final Unsafe unsafe = Unsafe.getUnsafe(); private static final long valueOffset; static { try { valueOffset = unsafe.objectFieldOffset (AtomicInteger.class.getDeclaredField("value")); } catch (Exception ex) { throw new Error(ex); } } private volatile int value;
That’s part of the source code in Atomic Integer. Here’s the value, where value uses the volatile keyword. What volatile can do here is to make multiple threads share variables, but the problem is to use vThe olatile will make VM optimization ineffective and inefficient, so use it when necessary, so don’t use the AtomicInteger class casually, but in use scenarios.
AtomicInteger instance” to use.
Here’s an example of using Atomic Integer in a multi-threaded situation, which borrows a piece of code from an IT home.
public class AtomicTest { static long randomTime() { return (long) (Math.random() * 1000); } public static void main(String[] args) { // Blocking queue, which can hold 100 files. final BlockingQueue<File> queue = new LinkedBlockingQueue<File>(100); // Thread pool final ExecutorService exec = Executors.newFixedThreadPool(5); final File root = new File("D:\\ISO"); // Completion of the sign final File exitFile = new File(""); // Atomic integer reading number// AtomicIntegerAtomic updates can be achieved in concurrency, synchronized is avoided, and performance is very high. final AtomicInteger rc = new AtomicInteger(); // Atomic integer, write number. final AtomicInteger wc = new AtomicInteger(); // Reader thread Runnable read = new Runnable() { public void run() { scanFile(root); scanFile(exitFile); } public void scanFile(File file) { if (file.isDirectory()) { File[] files = file.listFiles(new FileFilter() { public boolean accept(File pathname) { return pathname.isDirectory() || pathname.getPath().endsWith(".iso"); } }); for (File one : files) scanFile(one); } else { try { // The atomic integer incrementAndGet method adds 1 of the current value in atomic mode and returns the updated value. int index = rc.incrementAndGet(); System.out.println("Read0: " + index + " " + file.getPath()); // Add to blocking queue queue.put(file); } catch (InterruptedException e) { } } } }; // submitMethod submits a Runnable task for execution, and returns a Future representing the task. exec.submit(read); // Four writing threads for (int index = 0; index < 4; index++) { // write thread final int num = index; Runnable write = new Runnable() { String threadName = "Write" + num; public void run() { while (true) { try { Thread.sleep(randomTime()); // The atomic integer incrementAndGet method adds 1 of the current value in atomic mode and returns the updated value. int index = wc.incrementAndGet(); // Gets and removes the header of the queue and waits until the element becomes available (if necessary). File file = queue.take(); // Queue no object if (file == exitFile) { // Add "logo" again to let other threads exit normally. queue.put(exitFile); break; } System.out.println(threadName + ": " + index + " " + file.getPath()); } catch (InterruptedException e) { } } } }; exec.submit(write); } exec.shutdown(); } }
AtomicInteger use summary
AtomicIntegerIn the use of non-blocking algorithm to achieve concurrency control, in some highly concurrent programs is very suitable, but not every scenario is suitable, different scenarios to use different numerical classes.