[Concurrent interview questions] Process, thread? Parallel, concurrent?

1. What is the relationship between processes and threads?

  • In terms of quantity: a program has at least one process, and a process can have multiple threads.

For example: JAVA starts a JVM process when we start the main function, and the thread where the main function is located is the thread to which the process belongs, also called the main thread. Of course, multiple threads can also be started.

  • From the perspective of resources: A process is the basic unit for a program to apply for resources (such as memory space and file handles) from the operating system. A thread is the smallest unit that can be executed individually in a process. All threads in the same process share the resources of the process (such as memory space and file handle)

For example: the main function starts a JVM process, which will create a shared heap and method area resources for all threads, and each thread has its own resources, program counter, and method stack.
Insert picture description here
The last task to be completed by the thread is called a task .

2. Concurrent, parallel, indistinct?

First look at this picture:
Insert picture description here

  • From a software perspective: Concurrency is the completion of multiple tasks in an alternating fashion over a period of time. Parallelism is the way to accomplish multiple tasks in parallel.
  • From a hardware point of view: the processor can only run one thread at a time. If you want to run multiple threads (concurrent) within the same period of time, the processor is implemented by the technology of time slice allocation, that is, each thread is allocated Time slice, the time slice ends, save the thread state, thread switching, execute the thread of the next time slice. If parallelization is to be achieved, one processor will not work, and multiple processors will each need to process one thread at the same time.
Published 34 original articles · Likes0 · Visits 1089

Guess you like

Origin blog.csdn.net/qq_42634696/article/details/105023023