Atomicinteger, this is the class of an atomic operation integer provided by Java, which in my eyes is still a surprise, probably not so interesting as to be a simple volatile, which is my humble opinion. In fact, this guy is very useful, in common scenarios, such as count++ or ++count, which is insecure in the use of Java Multi-threading, and Atomicinteger is atomic (thread-safe), can be seen in the following example.
Package Org.jan.java.test;import Java.util.concurrent.executorservice;import java.util.concurrent.Executors; Import java.util.concurrent.timeunit;/** * About count++ in the case of multithreaded calls * * @author Jan */public class Counter {private volatile in T count = 0;/** * In order to ensure the accuracy of the data, multi-threaded cases need to add synchronized keyword </br> * Otherwise there will be unexpected results this is also an important embodiment of thread safety */public void increment () { count++;} private int GetCount () {return count;} /** * Here simulates an incremental task, incrementing the target to 100W */public static void Main (string[] args) throws Interruptedexception {final Counter Counter = New Counter ();//final atomiccounter Counter = new Atomiccounter (); int workcount = 1000000; Executorservice executor = Executors.newfixedthreadpool (ten); Long start = System.currenttimemillis (); for (int i = 0; i < Workcount; i++) {Runnable Runnable = new Runnable () {@Overridepublic void run () {counter.increment ();}}; Executor.execute (runnable);} Close the startup thread, perform the unfinished Task Executor.shutdown (),//wait for all threads to complete the task before proceeding to the next executor.awaittermination (Long.max_value, Timeunit.days); System.out.prINTLN ("Time Consuming:" + (System.currenttimemillis ()-start) + "MS"); System.out.println ("Execution Result: count=" + counter.getcount ());}}
It's not what we expected. 100 0000. Usually we need to add the Synchronized keyword to the count++ to ensure his correctness.
If we change the way, with Atomicinteger to replace count++, how to do?
public class Atomiccounter {private Atomicinteger count = new Atomicinteger (0);//After using Atomicinteger, you do not need to lock, or you can implement thread safety. public void Increment () {//Gets the current value and self-increment count.incrementandget ();} /** * Gets the current value * @return */public int GetCount () {return count.get ();} decrements public void Deincrement () {count.decrementandget ();}}
Replace the Counter object in the first code with the object of our Atomiccounter class, try his method, we find that the result is guaranteed 100w!
Atomicinteger There are other ways to check the API on the line. And it is not that only integer has such an atomic operation of the class, here is not the focus, interested in their own Baidu to go.
Gets the current value of publicfinal int get ()//takes the current value and sets the new value publicfinal int getandset (intnewvalue)//Gets the current value and self-increment publicfinal int Getandincrement ()//Get current value and subtract publicfinal int getanddecrement ()//Get current value and add expected value publicfinal int Getandadd (INTDELTA)
You say that he is such a feature, is not very attractive?
His performance seems to be very powerful, just like the first code, I looked at Ah, after the count++ method plus the Synchronized field and Atomicinteger increment method compared, Atomicinteger performance looks faster than the former! If you are interested, you can compare it with yourself.
Well.
Finally, thanks to the following blog's dedication, thanks to the full help of Baidu Huang
- http://my.oschina.net/i33/blog/50503
- http://blog.csdn.net/zz198808/article/details/8029405
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Java] Primary knowledge atomicinteger