preface

Hello, I’m YSwDQz. Is a new writer determined to use the most simple language to speak knowledge ~

Starting today, I’m going to cover the entire concurrency part of Java.

I wrote about this section because the concurrency part has always been the hard part in Java, and I’m pretty good at it. I hope I can help you!

Let’s cut to the chase!


First, let’s start with history.

Why do we use concurrency

1. Because Moore’s Law fails.

I don’t know if you’ve heard of Moore’s Law. It’s a law of hardware.

In 1965, when he was the director of fairchild semiconductor’s research and development laboratory, Moore was asked to write an observation paper for the 35th anniversary issue of Electronics magazine entitled “Filling the Integrated Circuit with More Components.” When Moore started mapping the data, he noticed a striking trend: each new chip contained roughly twice as much capacity as its predecessor, and each chip was created within 18-24 months of the previous one.

In layman’s terms, a dollar’s worth of computer power quadruples every 18 months.

If this law were always true, the performance of computers would skyrocket exponentially. Unfortunately, that trajectory has come to an end. This doubling of capacity in two years has slowed as more and more silicon circuits are packed into the same small space, generating more heat. And there are bigger problems, too. The circuits of the top chipmakers are now 14 nanometers, smaller than most viruses. But the global semiconductor industry development planning blueprint, Paul in Galveston, President of the association of gini (Paolo Gargini) said: “by 2020, with the fastest developing speed, we chip circuit can reach 2-3 nanoscale, however at this level can only accommodate 10 atoms, such equipment, can also be called a” device “?

So, it’s hard to make progress on a single computing unit, so how can research and development continue?

The answer is multi-core cpus

In order to further improve the computing speed, they no longer pursue the capability of a single computing unit, but integrate multiple computing units together to form a multi-core CPU.

In fact, it is a good idea: since it is difficult to improve the capability of a single cell, it is better to develop multiple cells and let them work together to handle the business.

Therefore, in the context of multi-core CPUS, concurrent programming has gradually become a trend, because it can maximize the performance of multi-core CPUS, but also delay the failure of Moore’s Law. It’s a hardware problem, but it’s up to the software developers to solve it.

2. Many businesses are more suitable for multi-threading.

In fact, concurrency is almost everywhere in our lives. Take the classic question:

Xiaoming gets up in the morning to wear clothes 4 minutes, boil water 10 minutes, read 8 minutes, blunt bean milk 1 minute, eat breakfast 5 minutes, want at least how many minutes?

If Ming were only a single-threaded computer, he could only start from scratch and do the same thing, but in fact many things can be done together, such as reading while waiting for the water to boil. Using multiple threads in this case would certainly save a lot of time.

By the way, some official concepts you should know

Advantages of concurrency:

  • Concurrency can more objectively represent the problem model;
  • Concurrency can make full use of the advantages of CPU core and improve the efficiency of program execution.
  • Concurrency takes advantage of the CPU’s inherent asynchrony with other hardware devices.

Disadvantages of concurrency (solutions will be covered in more detail in future articles) :

  • In high concurrency scenarios, frequent context switches can cost performance.

In an operating system, thread switching takes time, so it can even happen that using multiple threads is not as fast as a single thread.

  • Thread-safety issues, such as thread deadlocks.

This is a little hard to explain, but it’s also the most important part of concurrency. Please let me keep in suspense, I will publish a special article about ~


In addition, we need to know some basic concepts. This is the dullest part of the article. I’ll try to keep it simple.

Concurrent, parallel, synchronous, asynchronous

The concept of threads and processes is familiar. So let’s skip them and talk about concurrency first

Concurrency: Concurrency refers to the idea that only one command can be executed at a time, but multiple processes are concurrency. The concurrency effect is similar to that of multiple processes running at the same time.

When we say Java concurrency, we generally mean that each business activity has a low frequency, but there are a large number of business activities going on simultaneously

What is often confused with him is parallelism.

Parallelism: Parallelism refers to two or more events (or threads) occurring at the same time, in the true sense of different events or threads executing at the same time on different CPU resources (multi-core). In parallel, there is no concept of competing for CPU resources and waiting for execution like concurrency, because threads in parallel are distributed on different cpus.

(Multithreading is a means to achieve concurrency/parallelism.)

That is, if you use multiple threads on a server with a single CPU, you can only achieve concurrency, but if the server is multi-core, you can achieve parallelism. True parallelism can only take place on servers with multi-core cpus

Synchronous and asynchronous: Synchronous and asynchronous are generally used to describe methods. Synchronous means that if you don’t do all the code in my method, you can’t do any other method. If it’s asynchronous, it doesn’t block, it can execute the method at the same time as the other methods.

In other words:

Concurrency and parallelism emphasize that N people do the same thing without competing.

Synchronous and asynchronous emphasize whether the code should be “etc”, that is, whether the whole code should be blocked.


That’s the end of this post, thank you for your support ~ other posts are on the way and will be updated soon!