background
Concurrent programming is one of the important features of the Java language, and of course the most difficult content to master. Writing reliable concurrent programs is no small challenge. However, if we, as programmers, want to become more valuable, we need to chew some hard bones. Therefore, understand the basic theory and programming practice of concurrent programming and make yourself more valuable.
Complete PDF document, see the end of the article for free
Question 1: What is thread safety?
Thread safety issues refer to problems that cause dirty data or other unforeseen results when multiple threads simultaneously read and write a state variable without any synchronization measures. The primary synchronization strategy in Java is to use the Synchronized keyword, which provides a reentrant exclusive lock.
Question 2: What is the visibility issue of shared variables?
To talk about visibility, we first need to introduce the memory model in Java when multithreading processes shared variables.
The Java memory model stipulates that all variables are stored in the main memory. When a thread uses variables, it copies the variables in the main memory to its own working space or called working memory.
When a thread operates a shared variable, the operation flow is:
- · Thread first copies shared variables from main memory to its own workspace
- · Then process the variables in the workspace
- · Update variable value to main memory after processing
So what happens if threads A and B deal with a shared variable at the same time?
First of all, they will go through the above three processes. If thread A copies the shared variable to the working memory, and has updated the data but has not yet updated the main memory (the result may currently be stored in the current CPU register or cache), At this time, thread B copies the shared variables to its own working memory for processing. After processing, thread A updates its processing results to the main memory or cache. It can be seen that thread B is not processing the results of thread A, that is Say that the variable value processed by thread A is invisible to thread B, which is the invisibility problem of shared variables.
The reason why the shared variable memory is not visible is because the three-step process is not an atomic operation. Knowing that proper synchronization can solve this problem.
We know that ArrayList is thread-unsafe, because its read and write methods do not have a synchronization strategy, which will cause dirty data and unpredictable results. Let's explain how to solve them one by one.
This is thread unsafe
public class ArrayList<E>
{
public E get(int index) { rangeCheck(index);
return elementData(index);
}
public E set(int index, E element) { rangeCheck(index);
E oldValue = elementData(index); elementData[index] = element; return oldValue;
}
}
The number of positive characters is limited, so I can only show you the document pictures; there is a complete PDF collection method at the end of the article
Part of the concurrent programming knowledge map, you can see the end of the article if you need a complete one
Due to the limitation of the number of words in the article, only part of the document can be displayed here; I have compiled a complete PDF here, and friends in need can click three consecutive times; add me "VX Assistant" to get this document for free !