[Big Data] Study Notes 1 Java SE Chapter 9 Multithreading 9.1 Related Concepts (Understanding)

[Big Data] Study Notes

insert image description here

1 Java SE

Chapter 9 Multithreading

9.1 Related concepts (understanding)
9.1.1 Threads and Processes
  • Program : In order to complete a certain task and function, choose a set of instructions written in a programming language.

  • Software : One or more application programs + related materials and resource files constitute a software system.

  • Process : refers to an application program running in memory. Each process has an independent memory space. A process is also an execution process of a program and is the basic unit of a system running a program. A program running by the system means a process starts from creation, process until it dies.

  • Thread : A thread is an execution unit in a process, responsible for the execution of programs in the current process, and there is at least one thread in a process. There can be multiple threads in a process, and this application can also be called a multi-threaded program.

    In short: There is at least one application program in a software, one running of the application program is a process, and there is at least one thread in a process.

  • Interview question: A process is the smallest unit of operating system scheduling and resource allocation, and a thread is the smallest unit of CPU scheduling. Different processes do not share memory. The cost of data exchange and communication between processes is high. Different threads share the memory of the same process. Of course, different threads also have their own independent memory space. For the method area, the memory of the same object in the heap can be shared between threads, but the local variables of the stack are always independent. In addition, the complexity of switching between processes is much higher than that of switching between threads.

9.1.2 View process and thread

We can right-click on the task bar at the bottom of the computer -----> open the task manager, and you can view the progress of the current task:

  1. Each application runs as a process

insert image description here

Each row under Application and Background Process is a process.

  1. Multiple runs of an application are multiple processes

insert image description here

  1. A process contains multiple threads

insert image description here

9.1.3 Concurrency and Parallelism
  • Parallel : Refers to two or more events occurring at the same time (simultaneously). It means that at the same time, multiple instructions are executed on multiple processors at the same time. 【True Simultaneity】
  • Concurrency : Refers to the occurrence of two or more events within the same time period . It means that only one instruction can be executed at the same time, but the instructions of multiple processes are quickly executed in rotation, so that it has the effect of simultaneous execution of multiple processes on a macro level.

insert image description here

In the operating system, multiple programs are started, and concurrency refers to multiple programs running at the same time macroscopically for a period of time. In a single CPU system, only one program can be executed at a time, that is, these programs are microscopically It is a time-sharing alternate operation, but it gives the impression that it is running at the same time, that is because the time-sharing alternate operation is very short.

In a multi-CPU system, these programs that can be executed concurrently can be allocated to multiple processors (CPUs) to realize multi-task parallel execution, that is, each processor is used to process a program that can be executed concurrently. Multiple programs can then be executed simultaneously. At present, the multi-core CPU in the computer market is a multi-core processor. The more cores, the more programs are processed in parallel , which can greatly improve the efficiency of computer operation.

example:

  • Parallelism: multiple tasks are performed together, and then aggregated, for example: making instant noodles, boiling water in an electric kettle, tearing seasonings and pouring them into buckets

  • Concurrency: Multiple threads are accessing the same resource at the same time, and multiple threads are on one point, for example: spring festival ticket grabbing, e-commerce spike...

Note: A computer with a single-core processor must not be able to process multiple tasks in parallel , only multiple tasks can run concurrently on a single CPU. In the same way, threads are the same. From a macro perspective, threads run in parallel, but from a micro perspective, they run serially, that is, one thread runs one thread at a time. When the system has only one CPU, the threads run in parallel. Executing multiple threads in a certain order, we call this situation thread scheduling.

Single-core CPU: only concurrent

Multi-core CPU: Parallel + Concurrency

9.1.4 Thread Scheduling
  • time-sharing scheduling

    All threads take turns to use the CPU, and each thread takes up CPU time evenly.

  • preemptive scheduling

    Give priority to threads with high priority to use the CPU. If the threads have the same priority, one will be randomly selected (thread randomness). Java uses preemptive scheduling.

    • Detailed Preemptive Scheduling

      Most operating systems support concurrent running of multiple processes, and almost all current operating systems support running multiple programs at the same time. For example: Now we are using the editor and the screen recording software in class, and at the same time, we also open the drawing board, dos window and other software. At this time, these programs are running at the same time, "it feels like these software are running at the same time."

      In fact, the CPU (Central Processing Unit) uses a preemptive scheduling mode to switch between multiple threads at high speed. For one core of the CPU, only one thread can be executed at a certain time, and the switching speed of the CPU between multiple threads is faster than we feel, and it seems to be running at the same time.
      In fact, multi-threaded programs cannot improve the running speed of the program, but they can improve the running efficiency of the program.Make CPU usage higher

insert image description here

Guess you like

Origin blog.csdn.net/weixin_44226181/article/details/130480108