JAVA Concurrent Process VS Thread

Processes and threads

process:

Process can be understood as a whole running program

A process is a program with certain independent functions and is the basic unit of the operating system for resource allocation

Thread:

Thread is the basic unit of operating system scheduling

Process vs. Thread

  1. A program has at least one process, and a process has at least one thread

file
2. Threads are more finely divided than processes, with less execution overhead and higher concurrency

  1. A process is an entity with independent resources, and multiple threads in the same process share the resources of the process as shown below:
    file

  2. The JVM runs in a single process, and the threads in the JVM share the heap belonging to the process, so several threads can access an object in the heap at the same time, which leads to thread insecurity

  3. The thread has its own Stack space in the process, so a thread calling method and other local variables are thread-complete

to sum up

Process and thread are in a containment relationship, but multitasking can be implemented by multiple processes, multiple threads within a single process, or mixed multiple processes + multiple threads.

Which method to use depends on the characteristics of processes and threads.

Compared with multithreading, the disadvantages of multiprocessing are:

  • Creating a process is more expensive than creating a thread, especially on Windows systems;
  • Inter-process communication is slower than inter-thread communication, because inter-thread communication is to read and write the same variable, which is very fast.

The advantages of multi-process are:

Multi-process stability is higher than multi-threading, because in the case of multiple processes, a process crash will not affect other processes, and in the case of multi-threaded, any one thread crash will directly cause the entire process to crash.

The Java language has built-in multithreading support: a Java program is actually a JVM process, and the JVM process uses a main thread to execute the main()method. Within the main()method, we can start multiple threads. In addition, the JVM has other worker threads responsible for garbage collection.

Therefore, for most Java programs, when we say multitasking, we actually mean how to use multithreading to achieve multitasking.

Compared with single-threaded programming, the characteristic of multi-threaded programming is that multiple threads often need to read and write shared data and need to be synchronized. For example, when a movie is played, one thread must play the video and the other thread will play the audio. The two threads need to be coordinated, otherwise the picture and sound will not be synchronized. Therefore, the complexity of multi-threaded programming is high and debugging is more difficult.

The characteristics of Java multi-threaded programming are:

  • Multithreading model is the most basic concurrency model for Java programs;
  • Subsequent reading and writing of networks, databases, and Web development all rely on the Java multithreading model.

Therefore, you must master Java multi-threaded programming in order to continue to learn other content.

Guess you like

Origin blog.csdn.net/a159357445566/article/details/109250443