Remember came to hangzhou interview in March this year, a company’s technical director asked me such a question, say you tell me about what a thread-safe class, my in the mind think, ha ha, this I early back ok, sparse inside hua said a lot of, what would you say it again is thread-safe, and then GG, to be honest, we said thread safe all day, But do you really know what thread safety is? To be honest, I really knew very little about it before, so let’s talk about it today.
Before we talk about thread safety let’s talk about what a process is.
Let’s start with a picture
The programs we see running separately are independent processes, independent of each other.
The QQ, kugou player, computer housekeeper and so on in our picture above are all independent processes.
In other words, the minimum unit of execution in a process is the thread, and there is at least one thread in a process.
When it comes to multithreading, there are two concepts, serial and parallel, that we can understand better.
So-called serial is relative to A single thread to perform multiple tasks, and we had to download files, for example, we download multiple files, it is in the serial according to certain order to download, that is to say, after must wait for download A, can begin downloading B, they cannot overlap in time.
Parallel: download multiple files, open multiple threads, multiple files are downloaded at the same time, here is strictly at the same time, parallelism is overlapping in time.
After the understand the two concepts we will say what is multithreaded, for example, we open the key, tencent tencent key itself is a program that is to say, it is a process, it has a lot of function, we can see below, can be killing virus, clearing up the rubbish, computer speed, and many other functions, according to the single thread, Clearing up the rubbish and virus killing you want, then you have to finish one thing to do one thing, there is a sequence of execution, if is multithreaded, we actually when clearing up the rubbish can accelerate killing virus, computers and so on other operation, this happened at the same time in a strict sense, no order execution.
To sum up: a process runs with multiple threads.
After understanding this problem, we need to understand a problem that we have to consider using multithreading, thread safety, today we don’t talk about how to ensure the safety of a thread, let’s talk about what is thread safety? Because I was asked this question in the interview before. To be honest, I didn’t know this question very well before. It seems that we only learn how to ensure the safety of a thread, but we don’t know what the so-called safety is!
Since this is a thread-safe issue, there is no doubt that all the risks are caused by multi-thread access. We need to make sure that our program still behaves as expected when accessing multiple threads. Let’s take a look at the following code.
Integer count = 0;
public void getCount() {
count ++;
System.out.println(count);
}Copy the code
A simple piece of code, let’s count the number of times this method is accessed, multiple threads access at the same time will not have any problems, I opened the 3 threads each loop 10 times, get the result
And you can see that there are two 26s here, so why does that happen, and it’s clear that our method is not thread-safe at all, and there are A number of reasons why that might happen, and let’s say the most common one is that thread A, when we enter the method, gets the value of count, Thread B also comes in, so thread A and thread B get the same count as each other.
So we can see that this is not really a thread-safe class, because they all need to operate on the shared variable. It is quite complicated to give a clear definition of thread-safe problem. Let’s sum up what thread-safe is based on our program.
When multiple threads access a method, no matter how you call it or how those threads are executed interchangeably, we don’t need to do any synchronization in the main program, and the resulting behavior of the class is the correct behavior we assume, so we can say that the class is thread-safe.
PS: Do you know of any ways to keep threads safe?