The widespread application of concurrent processing is to make the AMDAH1 law instead of Moore's law become the root cause of the power of computer performance development, and is the most powerful weapon for human to crush computer computing ability.
Concurrency does not have to be multi-threaded, multi-process can also, but Java inside talk about concurrency, most of the thread is not open relationship.
1. Threading implementation
Threads are more lightweight than the process of scheduling execution units, in Linux, threads and processes are no different, the only thing is in the address space, the switch of the virtual memory space is still the same, but the process switching is different.
Currently the mainstream operating system is provided by the thread implementation, Java is provided by the thread implementation method is native, because different hardware and operating system provides thread scheduling method and is not the same, so Java does not use the peace platform-independent unified means to achieve.
The main 3 ways to implement threading: using the kernel thread implementation, using the user thread implementation, using the user line loads lightweight process hybrid implementation.
Implementation of kernel threads
Kernel threads (KLT) are threads that are supported directly by the operating system kernel, which is the kernel that completes thread switching.
Programs generally do not use kernel threads directly, but instead use a high-level interface of kernel threads-lightweight processes (LWP), the lightweight process is what we are talking about, a lightweight process that corresponds to 1:1 of the kernel thread.
Advantages:
Direct kernel support, created and revoked by the operating system kernel. The kernel maintains the context information of processes and threads, as well as thread switching. A kernel thread is blocked due to I/O operations and does not affect the operation of other threads.
Disadvantages:
1, the operation of the thread, creation, synchronization and so on need system calls, and system call cost is relatively high, need in the user state and the kernel state to switch back and forth.
2. Each lightweight process requires a kernel thread to support and consumes a certain amount of kernel resources.
User Thread Implementation
A user thread is a thread implemented in a user program that does not require kernel support and is not dependent on the operating system core, and the application process uses line libraries to provide functions to create, synchronize, dispatch, and manage threads to control the user thread.
No need for user-state/kernel-mindset switching, fast, operating system kernel does not know the existence of multithreading, so a thread blocking will make the entire process (including all its threads) blocked. Programs that use user threads are generally more complex, and Java has been used, but eventually it is abandoned.
Advantages :
Switching by the user state program to control the kernel switching, without the need for core interference, less in and out of the kernel state consumption.
Disadvantages :
Multi-core processors are difficult to tell if threads are mapped to other processors, and single-threaded blocking can cause the process to block.
User line loads lightweight threading hybrid implementation
In this mixed mode, there are both user threads and lightweight processes. User threads are still completely built into user space, so the creation, switching, and destruction of user threads are still cheap and can support large-scale user-thread concurrency.
The operating system provides a lightweight process that is supported as a bridge between the user thread and the kernel thread, and the user thread's system call is done through a lightweight thread, greatly reducing the risk of process blocking. The user thread and the lightweight process are n:m many pairs of relationships.
Java was implemented based on user threads prior to JDK 1.2, After 1.2, the OS-based native threading model was implemented on every platform, for example, in both Windows and Linux, with a one-to-one threading model implementation, in the Solaris platform, with a single-or many-to-many implementation (Solaris Supports one-to-one and many-to-many simultaneously).
2. Thread scheduling
Thread scheduling mainly refers to the process that the system assigns the processor to the thread, which is mainly divided into: collaborative thread scheduling and preemptive thread scheduling.
Co-scheduling
The execution time of cooperative scheduling is controlled by the thread itself, and after the thread completes its work, the active notification system switches to another thread. This is how the "synergy process" like Lua is implemented.
Advantages:
The implementation is simple, the thread has done its own thing after the thread switch, the switch operation to the thread itself is known. No synchronization issues
Disadvantages :
Thread execution time is not controllable, and if a thread is stuck with a problem, it can cause the program to become blocked.
2 preemptive thread scheduling
In preemptive thread scheduling, each thread is assigned the execution time by the system, and the thread's switchover is not determined by the thread itself.
Advantages:
The execution time of the thread is controllable, and no single thread blocking causes the entire process to block.
Java is the use of preemptive thread scheduling, in addition, Java can also be set priority to the thread to recommend the system to some of the threads to allocate a little more time, but not very reliable, thread scheduling ultimately depends on the operating system.
State transitions
Java defines 5 thread states, at any point in time, and only one of the threads has one state.
By the above we know that the Java thread is mapped to the operating system kernel thread, if blocking or waking a thread, all need the operating system to help complete, this need to switch from the user state to the kernel mentality, because the state of the transition this step will consume a lot of processor time, need to use caution, The specific how to use carefully, whether there is room for optimization, we will explain the next chapter.
Java multithreading-how threads come in