preface

Text has been included to my lot warehouse, welcome Star:https://github.com/bin392328206/six-finger plant a tree is the best time ten years ago, the second is writing a blog on the way now encourage everyone in technology

omg

Remember that every interview comes with a question: What are the thread-safe classes? I thought in my heart, this I have already carried on the back, spluttered said a lot. But do we really know what thread safety is? I really know little about it before, so let’s talk about it today.

Thread safety Tips Today we will take you to understand thread safety, and then I will talk about thread safety


What is a process

In a computer, there are many programs that run independently, and each program has a separate process, and the processes are independent of each other. For example, IDEA,Navicat and PostMan are shown in the figure below


What is a thread?

Processes depend on threads to perform tasks. In other words, the smallest unit of execution in a process is a thread, and there is at least one thread in a process.

What is multithreading? When we talk about multithreading, there are two concepts, serial and parallel, and if we understand that, we’ll understand multithreading better.

Alleged serial, in fact is relative to A single thread to perform multiple tasks, and we had to download files, for example: when 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.

With these two concepts in mind, let’s talk about what multithreading is. For example, we open Tencent Housekeeper, Tencent housekeeper itself is a program, that is to say, it is a process, it has a lot of functions, we can see the picture below, can kill viruses, garbage cleaning, computer acceleration and many other functions.


What is thread safety?

Since this is a thread-safe issue, there is no doubt that all of the pitfalls arise in the case of multiple threads, which means that we need to ensure that our program still behaves as expected when accessing multiple threads. Let’s look at the following code.

public class ThreadDemo {
static Integer count = 0; public void getCount() { count ++; System.out.println(count+Thread.currentThread().getName()); } public static void main(String[] args) { for (int i=0; i< = 3; i++){ new Thread(()-> { for (int a=0; a< = 10; a++){ ThreadDemo threadDemo = new ThreadDemo(); threadDemo.getCount(); } }).start(); }Copy the code

Copy the code

"C: Program Files\Java\jdk1.8.0_172\bin\java.exe" -dVisualvm. id=77484934182900 "-JavaAgent :E:\manuls\IntelliJ IDEA 2019.3\lib\idea_rt.jar=64836:E:\manuls\IntelliJ IDEA 2019.3\bin" -dfile. encoding= UTF-8-classpath "C:\Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ charsets jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ deploy the jar. C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ ext \ access - bridge - 64. The jar. C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ ext \ cldrdata jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ ext \ DNSNS jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ ext \ jaccess jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ ext \ JFXRT jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ ext \ localedata jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ ext \ nashorn jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ ext \ sunec jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ ext \ sunjce_provider jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ ext \ sunmscapi jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ ext \ sunpkcs11 jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ ext \ zipfs jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ javaws jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ jce jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ JFR jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ JFXSWT jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ jsse jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ management - agent jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ plugin jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ resources jar; C: \ Program Files \ Java \ jdk1.8.0 _172 \ jre \ lib \ rt jar; E:\code\sixfinger-leadnews\service-mycat\target\classes; E: \ code \ sixfinger leadnews \ service - mycat \ libs \ mycat - server - 1.6 - the jar. D: \ repository \ org \ springframework/boot/spring - the boot - starter - log4j2\2.1.5 RELEASE \ spring - the boot - starter - log4j2-2.1.5. RELEASE .jar; D: \ repository \ org \ apache \ logging, log4j, log4j - slf4j - impl \ 2.11.2 \ log4j - slf4j - impl - 2.11.2. Jar; D: \ repository \ org \ slf4j \ \ 1.7.26 \ slf4j slf4j - API - API - 1.7.26. Jar; D: \ repository \ org \ apache \ logging, log4j, log4j - API \ 2.11.2, log4j - API - 2.11.2. Jar; D: \ repository \ org \ apache \ logging, log4j, log4j - core \ 2.11.2, log4j - core - 2.11.2. Jar; D: \ repository \ org \ apache \ logging, log4j, log4j - jul, 2.11.2 \ log4j - jul - 2.11.2. Jar; D:\repository\ org.slf4j \jul-to-slf4j\1.7.26\jul-to-slf4j-1.7.26.jar" com.hima.threaddemo 14 5 6 7 8 9 10 11 12 13 3 14  15 2 1 17 16 19 18 21 23 24 25 26 27 28 29 20 22 30 31 32 33 35 36 37 38 34 39 41 40 42 43 Process finished with exit code 0Copy the code

As you can see, there are two ones here, which clearly indicates that this method is not thread-safe at all, and there are many reasons for this problem.

The most common one is when thread A enters the method and gets the value of count. As soon as thread A reads the value of count, thread B enters the method and gets the same value of count as thread B.

So we can see from this that this is really not a thread-safe class because they all need to operate on this shared variable. In fact, to give a clear definition of thread safety problem, or quite complex, let’s summarize what is thread safety according to our program.

When multiple threads access a method, we don’t need to do any synchronization in the main program, regardless of how you call it, or how those threads are executed interchangeably, and the resulting behavior of the class is what we expect it to be, we can say that the class is thread-safe.

Addressing thread safety

I won’t go into the various thread-safety solutions in this article because there are too many. Instead, I will talk about how often we cruds encounter thread-safety problems in real projects.

The bar is thread safe

In the morning, my colleagues in the company looked at the source code of the collection, and THEN I went over and discussed it with them, and I found a problem, which is that I found that the system I maintain, and the system I developed, I rarely talk about thread safety, you know, ArrayList,HashMap, everybody says thread safety is not safe, But I think about it for a long time and I don’t see a scenario where I have to worry about thread safety at all, is that most of my collections are defined inside methods, and that’s defined inside methods, and we cruD have very little thread memory and that’s defined using multiple threads, so it just feels like our business development, It is difficult to understand multithreading at a deep level.

You can think about it for a moment, is it controller, service, DAO, layer by layer, a request comes in directly into a method, which is often referred to as the stack frame. For the collection container defined in the stack frame, we also rarely define multi-threading out of the processing, for business, is really few, maybe you would say we can use multi-threading in a method to query, to update. That’s true, but do you think that if you start with a collection that you have to operate on, one for each thread, what are the thread-safe places that you have to think about in a real business scenario?

Learning from source

To tell the truth, today to discuss with your colleagues, run into a lot more, “he said You will understand, he says the things by yourself to the cumulative, an example but also suddenly can’t say, colleagues said I experience too little, too little, colleagues be Daniel more than 5 years experience, almost is the architect, I think I may be really experience is not enough, Source in addition to the container, seen in all sorts of Spring source, the other is seen less, but colleagues have other HuaHua really, why don’t we own program of the memory when the database, if you put data into your application, so you have to think about these things, such as seconds kill scenes On the method of inventory reduction design a current-limiting algorithm, That’s where thread safety comes in.

At the end

Ha ha, the author is not a title party, just say oneself to think of something, I think although we use less, but still it is necessary to learn the knowledge, if you write the middleware, you write a database, these are all things must be considered, if it’s just CRUD, you need to understand these is really very few, but we have to grow, You have to learn it.

Tips everyone can leave a message below, I have a look at everyone’s business scene, small 66 May be really encountered less, only like you big guy learning to learn.


Daily for praise

Ok, everybody, that’s all for this article, you can see people here, they are real fans.

Creation is not easy, your support and recognition, is the biggest motivation for my creation, we will see in the next article

Six pulse excalibur | article “original” if there are any errors in this blog, please give criticisms, be obliged!