JUC Concurrency Series (1): What? I heard you confuse concurrency and parallelism!

It is not difficult to run a program; the difficult thing is how far the program can run! —— a fierce seed

Insert picture description here

Before jumping into the topic, if you don’t understand threads and processes, make sure to make up (the link has already been submitted). Just like the above quoting a fierce seed, learning concurrency not only determines how far the program can run, but also determines you How far can you go.

Understanding multithreading lies in a deep understanding of multitasking, processes, multithreading, and threads

The little friends who ran to read the above article, did they come back full of question marks, curious about how many threads Java itself has by default?

Presumably you may answer, of course it is 1, mian thread, learn hello world then you will know.

Unfortunately, it was wrong!

The correct answer is: Java has 2 threads by default, one is main and the other is GC. (There is a question mark on this, don’t worry, you will know about JVM at that time. The premise is to follow me, hehe)

It seems to be far away (but is it not good to learn more knowledge?)

Insert picture description here

The concept of concurrency is actually not unfamiliar. If your project has a larger amount of data, you must have encountered it. (Even if not, the interview is a must-know-knowledge point, because it is very important in the enterprise. Whether it is a large amount of project data involved, it is a last resort, or it is a hurdle that cannot be avoided. )

This article is the first familiarity with concurrency series (1), talking about concurrency, in fact, talking about concurrent programming.

The essence of concurrent programming: make full use of CPU resources.

Let me just throw a question, even if you know concurrency, what is parallelism?

Insert picture description here

1. What is concurrency and parallelism?

Concurrency: Multiple threads operate on the same resource. (Can be understood as alternate execution)

Refers to the execution of only one instruction at the same time, CPU scheduling will have the illusion that it is executing at the same time, not at the same time, in fact, it is executed in rotation (described in the in-depth multithreading series), that is, fast alternate execution.

Parallel: simultaneous execution.

Refers to the simultaneous execution of multiple instructions at the same time, both macro and micro levels are executed together. (I have talked about in-depth multithreading)

Multi-core CPU is no longer the illusion of such fast alternate operation, multi-core CPU is really realizing simultaneous execution of multiple threads.

We can take a look at our processor.
Insert picture description here

2. Code to get the number of CPU cores

public class demo{
    
    
    public static void main(String[] args) {
    
    
        System.out.println(Runtime.getRuntime().availableProcessors());
    }
}

Running result (the number of CPU cores obtained is the same as we saw in the computer management.)

Insert picture description here

Three. Finally

At the end, for a better reading experience, I put everything I want to say below, hehe.

I am a seed of decisions based on my will, serious share what I wrote blog has been the same creed.
If you can read this blog post, it means that we are still very destined; I hope it can bring you some help, the creation is not easy,
take away the knowledge of my article, your three consecutive leave, like, comment, follow , Is my biggest motivation.

Guess you like

Origin blog.csdn.net/A_hxy/article/details/108632747