011 Thread Safety Issues

I. Overview

Earlier, we said that thread safety is our primary consideration in concurrent design.

  So what exactly is a concurrency problem?

  See the following example (classic example):      

public class Problem {
    
    private int count = 0;
    
    public static void main(String[] args) throws Exception {
        Problem demo = new Problem();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for(int x =0;x<10000;x++) {
                    demo.add();
                }
            }
            
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for(int x =0;x<10000;x++) {
                    demo.add();
                }
            }
            
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out .println ( " Get the final result count : " + demo.count );
        
    }
    private void add() {
        count++;
    }
}

The above code describes that two threads jointly increment a count, 1000 times per person, but the result of the final run is very disappointing, almost every time it is not 2000 times.


 

2. Analysis of the problem

  In fact, the most fundamental problem is that the state of the count obtained by the two threads is inconsistent, that is to say, the count++ operation is not an atomic operation at all.

    This allows a thread to acquire an intermediate state of another thread.

  This may be abstract, simply put, operations on count should be sequential.


 

3. Conditions for the occurrence of security problems

  Above we have shown security issues, but when will security issues occur? After all, how can we reduce concurrency concerns if there are no security issues in our program, which can help us simplify the problem.

  Conditions under which thread safety occurs:

  [1] Multi-threaded concurrency

  [2] Compete for the same resource

  [3] Perform non-atomic operations on competing resources [common is inconsistent read and write]

Explain one of the above conditions:

  The first two will not be explained. The third condition will be explained.

  Atomicity:

    This concept of atomicity is consistent with our concept in transactions, that our operations should be a complete individual, either not happening or completely happening. In it, there will be no interruptions.

  Taking the above example as an example, the count++ operation is not atomic, because this statement will be divided into multiple statements at the bottom layer for execution, and each statement can be interrupted.


4. Problem solving

  The most common way to solve the thread safety problem is to synchronize, of course, there are many ways to synchronize,

    Such as locking, synchronized, CAS, etc., but their core is one point to ensure atomicity.

Guess you like

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