Overview of threading basics

1. Concept term

Process: Process is usually a synonym for program and application. However, a single application seen by the user may actually have a series of cooperative processes ( cooperating processes), for example, a process copy can be created by fork on Linux . In the simplest case, an application is a process. Most Java virtual machine implementations run as a single process, but we can also create additional processes through ProcessBuilder.

 

Thread: Thread is also sometimes called 轻量级进程. But creating a thread usually consumes much less resources than creating a process. Multiple threads in a process will share the resources of the process, and will also have their own private resources. Threads must exist in the process, and each process must have at least one thread as the entry of the program, called the main thread.

 

Concurrency: On a computer with a single-core CPU, even if the operating system supports multitasking running at the same time, in fact, at any time, only one thread of a process is running, because a CPU can only process one instruction at a time. It seems that multiple tasks executing at the same time are actually preemptively overlapping and alternately executing, which is concurrency.

 

Parallelism: On computers with multi-core CPUs, each CPU core can execute different instructions at the same time, enabling a parallel multi-threaded execution model.

 

2. Java and threads

The number of threads that can be created in a process is not only limited by the use of different operating systems 线程模型, but also limited by system resources such as memory size, because each thread has its own stack memory space. Of course, for the threads created by the Java process, the stack memory space is mainly affected by the memory size that the JVM can use, not the total memory of the operating system.

 

Threads in Java are managed by the JVM, and how it corresponds to the threads of the operating system is determined by the implementation of the JVM. HotSpot on Linux 2.6 uses the NPTL mechanism, and there is a one-to-one correspondence between JVM threads and kernel threads. What is the benefit of doing this? The scheduling of threads is completely handed over to the operating system kernel. Of course, the jvm also retains some strategies that can affect its internal thread scheduling.

 

The advantages and costs of multithreading

Multithreading can bring the following advantages to programming :

        * Higher resource utilization

          For example, in specific processing after file/content retrieval through disk or network IO, the disk/network IO operation needs to wait for a relatively long time, and the CPU is in an idle state during this time. IO operations (here mainly refers to disk IO) are often performed through hardware direct access devices (DMA), that is to say, the CPU only needs to send an instruction to the DMA to perform the corresponding IO operation. In an instant, the CPU can do other things after sending it. If the multi-threading method is adopted, the CPU can be made to process other logic that can be processed during the waiting process, thereby greatly improving the utilization rate of the CPU.

 

        * The program responds faster

         In C/S programming similar to desktop applications, the time-consuming tasks submitted by users often do not need to be returned immediately, but the user interface cannot be in an unavailable state of waiting all the time. Users can perform other operations during this time. After the task is completed, the user is actively notified. This multi-threaded design provides users with a faster and more elegant experience.

 

Multithreading not only brings benefits to programming, but also comes with some costs :

         * Programming is more complicated

           If access to shared data is involved in multithreaded concurrent program design, how to prevent the confusion of shared data caused by concurrent access will be very difficult and a factor that complicates program design.

 

         * Context switching overhead for multithreading

           When the CPU switches from executing one thread to executing another thread, it needs to first store the local data, program pointer, etc. of the current thread, and then load the local data, program pointer, etc. of another thread, and finally start execution. This switch is called a "context switch" (" context switch"). The CPU will execute a thread in one context, and then switch to another context to execute another thread. Context switching is not cheap. Context switches should be reduced if not necessary.

 

         * Thread stack space increases memory resource consumption

          After each thread is started, the process also allocates a certain amount of memory to the thread to store its own private data. The area of ​​memory that the JVM allocates to each thread is called 线程栈内存. By default, the size of the stack memory is 1M. That is to say, every time you start one more thread, at least 1M more memory resources are consumed.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326797996&siteId=291194637