Process Vs Thread, concurrent parallel Vs

Process Vs Thread, concurrent parallel Vs

CPU cores relationship with the number of threads?

For a CPU, the number of threads> = number of cores
that is, a core may be at least one thread, but Hyper-Threading technology, a CPU core can run two threads simultaneously.

  • It may be equivalent to the number of threads at a time, CPU of the number of concurrent tasks.

Process Vs Thread

What is the process?
  • Process is a function of an independent program, a process executing on a dynamic data set, the system is a basic unit for resource allocation and scheduling.
  • In computer architecture early in the design process-oriented, the basic process is the execution entity of the program.
  • In computer architecture for contemporary design of the thread, the thread is the process of container.
  • Program is instruction, organization of data and its description of the process is a solid program.

The first point

Process is an entity. Each process has its own address space, under normal circumstances, including the text area (text region), the data region (data region) and stack (stack region). Code is executed by a processor stored text area; dynamically allocated memory area stores data used during execution process variables; stack area stores instructions and local variables of the calling procedure activities.

Second point

Process is an "execution procedures." Program is a lifeless entity (operating system performs the) only when the processor attached to the program of life, it can become an active entity, we call for the process.

In short it can be summarized as:

  • Process is a process of implementation of the program, is the basic unit of the system to run the program, so the process is dynamic.
  • The system is running a program that is created from a process, run to the demise of the process.
What is a thread?
  • A thread is the smallest unit of an operating system capable of operation scheduling.
  • Be included in the process, the actual operation of the unit process.
  • A thread refers to a single control flow of a process sequence, a process can be complicated by a plurality of threads, each thread in parallel to perform different tasks.

Important point

Sharing program memory space between threads (that is, where the process memory space).
A standard thread by the thread ID, the current instruction pointer PC, registers and stack components. And the process by the memory space (code, data, process space, open files) and one or more of threads.

Processes and threads in Java

The reason for finishing so much, because I think only a few words to sum up the words really hard to understand what the process of what is called a thread, everyone read these concepts naturally understand.
Next we come back in JAVA.

public class MultiThread {
    public static void main(String[] args) {
        // 获取 Java 线程管理 MXBean
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        // 不需要获取同步的 monitor 和 synchronizer 信息,仅获取线程和线程堆栈信息
        ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false);
        // 遍历线程信息,仅打印线程 ID 和线程名称信息
        for (ThreadInfo threadInfo : threadInfos) {
            System.out.println("[" + threadInfo.getThreadId() + "] " + threadInfo.getThreadName());
        }
    }
}

The above procedure follows output, print the names of all participating threads

[5] Attach Listener //添加事件
[4] Signal Dispatcher // 分发处理给 JVM 信号的线程
[3] Finalizer //调用对象 finalize 方法的线程
[2] Reference Handler //清除 reference 线程
[1] main //main 线程,程序入口

In Java, when we start the main function, in fact, it is to start the process of a JVM, while the main function where several threads threads and above all a part of this process.

The difference between processes and threads
  1. A thread is the smallest unit of program execution, and the process is the smallest unit of the operating system of allocating resources;
  2. A process consisting of one or more threads, the thread is a different process to implement the road code
  3. Between processes independent of each other, but the program memory space shared between threads in the same process (including code segments, data sets, stack, etc.) and some of the process-level resources (such as open files and signals, etc.), within a process thread is not visible in the other process;
  4. Each process has its own code and data space (application context), switching between programs have a greater overhead; lightweight process thread can be seen, shared code and data with a certain threads within a process space, each thread has its own separate runtime stack and program counter (PC), small switches between threads overhead.
From the point of view of the relationship between the Java JVM processes and threads

Concurrent Vs Parallel

Complicated by
  • When more than one thread to each thread in operation, if the system is only one CPU, then it simply can not be true at the same time more than one thread, it can only CPU time is divided into several periods, then the period of time allocated perform, in a period of the code the thread is running, the other thread is in a suspended state. This way we call concurrent (Concurrent).
parallel
  • When the system has more than one CPU, then it is possible to operate the thread non-concurrent. When a CPU executes a thread, the other CPU can execute another thread, two threads and do not seize the CPU resources, may be performed simultaneously, we call such a manner parallel (Parallel).
Difference between the two
  • Refers to two or more concurrent events occurring at the same time interval, it refers to two or more parallel events occur at the same time.

Since the above reference part

  • Baidu Encyclopedia
  • https://snailclimb.top/JavaGuide/#/?id=%e5%b9%b6%e5%8f%91
  • https://blog.csdn.net/kuangsonghan/article/details/80674777

Standing on the shoulders of giants -

Published 32 original articles · won praise 13 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_43519048/article/details/103378596