[Java] The concept of Java threads

http://www.weixueyuan.net/view/6024.html

Unlike most other computer languages, Java has built-in support for multithreaded programming.


A multithreaded program contains two or more parts that run concurrently. Each such part of the program is called a thread, and each thread has an independent execution path. Therefore, multithreading is a special form of multitasking.

You must know multitasking, because it is actually supported by all modern operating systems. However, there are two distinct types of multitasking: process-based and thread-based. It is very important to recognize the difference between the two.

For many readers, process-based multitasking is the more familiar form. A process is essentially an execution program. Therefore, the feature of process-based multitasking is to allow your computer to run two or more programs at the same time. For example, process-based multitasking allows you to run the Java compiler at the same time when using a text editor. In process-based multitasking, the program is the smallest unit of code assigned by the scheduler.

In a thread-based multitasking environment, the thread is the smallest unit of execution. This means that a program can perform the function of two or more tasks at the same time. For example, a text editor can format text while printing. Therefore, multi-process programs deal with "big pictures", while multi-threaded programs deal with details.

Multi-threaded programs require less management costs than multi-process programs. Processes are heavyweight tasks and need to allocate their own independent address space. Inter-process communication is expensive and limited. The conversion between processes is also very expensive. On the other hand, threads are lightweight players. They share the same address space and share the same process. Communication between threads is cheap, and switching between threads is also low-cost. When a Java program uses a multi-process task processing environment, the multi-process program is not controlled by Java, and multi-threaded is controlled by Java.

Multithreading helps you write efficient programs with maximum CPU utilization, because idle time is kept to a minimum. This is essential to the interactive network interconnection environment where Java runs, because free time is public. For example, the data transfer rate of the network is much lower than the processing capacity of the computer, and the read and write speed of local file system resources is much lower than the processing capacity of the CPU. Of course, user input is also much slower than the computer. In a traditional single-threaded environment, your program must wait for each such task to complete before it can execute the next step—even though the CPU has a lot of idle time. Multi-threading enables you to obtain and make full use of these idle time.

Guess you like

Origin blog.csdn.net/michellechouu/article/details/48751583