AtomicInteger

Atomic Integer (AtomicBoolean, AtomicInteger, AtomicLong, AtomicReference (reference object)) series uses synchronized combined with volatile, and CAS algorithm to achieve the safety of marked variables in multi-threading.

 

 

The CAS (compare-and-swap) algorithm guarantees atomicity.

 

The principle of the CAS algorithm is that it contains three values: memory value A estimated value V update value B If and only if V == A, V = B; otherwise, no operation will be performed.

 

 

1, the thread safety of the code block (synchronized, lock)

2. Thread safety of variables: thread safety of common values ​​(volatile, atomic*), thread safety of data structures (concurrenthashmap, threadlocal, copyonwriterarraylist, blockingqueue)

 

 

 

 

The AtomicInteger class ensures thread-safe usage

 

J2SE 5.0 provides a set of atomic classes to help us simplify synchronization. The basic working principle is to use the synchronized method to realize the addition, subtraction, assignment (update) operation of a long, integer, object. For example, the ++ operator AtomicInteger can atomically increment the integer it holds. Program code that needs to access two or more atomic variables (or perform two or more operations on a single atomic variable) usually needs to be synchronized so that both operations can be treated as one atomic unit.

java multithreading usage - using AtomicInteger
Let's take a look at the powerful functions of AtomicInteger through the comparison of two simple examples
class  Counter {
private  volatile  int  count =  0 ;
 
public  synchronized  void  increment() {
count++;   //若要线程安全执行执行count++,需要加锁
}
 
public  int  getCount() {
return  count;
}
}
 
class  Counter {
private  AtomicInteger count =  new  AtomicInteger();
 
public  void  increment() {
count.incrementAndGet();
}
        //使用AtomicInteger之后,不需要加锁,也可以实现线程安全。
public  int  getCount() {
return  count.get();
}
}
From the above example we can see that it is very safe to use AtomicInteger
So why not use the counter to add itself, such as count++, because this kind of counting is thread-unsafe, and the statistics will be wrong during high concurrent access, and why can AtomicInteger achieve as much without being chaotic, and handle high concurrency with ease Woolen cloth?
This is achieved by hardware providing atomic operation instructions. Less overhead and faster in non-competitive situations. The atomic operation classes implemented in java.util.concurrent include:
AtomicBoolean、AtomicInteger、AtomicLong、AtomicReference。

  In addition, the bottom layer is the result of the combined effect of volatile and CAS:

     1. First use volatile to ensure memory visibility.

     2. Then the CAS (compare-and-swap) algorithm is used to ensure atomicity.

       The principle of the CAS algorithm is that it contains three values: memory value A estimated value V update value B If and only if V == A, V = B; otherwise, no operation will be performed.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326269994&siteId=291194637