1. Overview
As we all know, threading- thread is a more lightweight scheduling unit than process-Progress . In simple terms, the benefits of introducing threading are:
The resource allocation and execution Schedule of a process can be separated, and each thread can share process resources (memory address, file I/O, etc.) and dispatch independently.
Thread Implementation method:
Mainstream operating systems implement threads , and programming languages generally provide uniform API operations on threads . So how does the programming language go about invoking the system thread ? There are 3 main ways of doing this:
use kernel thread-Kernelthread.
A pair of thread models , this most important, is detailed below.
User thread is used.
A pair of n threading model , the user program fully simulates the behavior of the thread, the code when the operation of the thread is the user program simulation of the thread, the operating system kernel can not perceive you do create, dispatch and other threading operations. Many programming languages were originally used in this way, but now basically give up this.
Implemented using user thread + lightweight process blending.
N for the M threading model , there are also many languages that use a hybrid implementation.
2. Java Threading Model
Selection of the JVM threading model:
First, the JVM specification does not qualify which threading model the JVM will use, and what threading model the JVM uses is related to many factors including the OS;
Second, the threading model affects only the concurrency and operational costs of the thread.
So the threading model is transparent to our programmer code.
In SUN JDK:
For the official JDK, the Sun JDK uses a two-thread model because the WINDWOS and Linux operating systems (mainstream) provide only a couple of thread models . Other JDK can have different implementations (such as the Solaris JDK can take N-to-M models).
kernel threading Mode:
In the three-way scenario described above, the SUN JDK uses the kernel thread . The thread scheduling process in this way is as follows:
Scheduling process:
First, from the OS side: If a thread scheduler (Scheduler) is included in the operating system kernel, the kernel can manipulate the scheduler to schedule kernel threads to map thread tasks to individual CPUs. So, this kernel is called multi-threaded kernel .
Second, from the program: The application cannot use kernel threads directly, only to use a high-level interface of kernel threads- lightweight process (LWP) . For programming languages, the fact that they operate is that the LWP,LWP is the thread that can be called at this point.
Finally, the mapping between lightweight processes and threads is 1:1, so this approach is called a pair of thread models .
3. Java Thread Scheduling
Thread scheduling refers to the process by which a system allocates processor access for a thread, and there are two main scheduling methods:
Collaborative thread scheduling (cooperative threads-scheduling)
This way is the original way that one thread finishes executing the notification to another thread. Is rarely used and is prone to clogging.
preemptive thread scheduling (preemptive threads-scheduling)
The mainstream approach, in which the system allocates execution time for each thread based on a complex set of rules, the thread is not switched by the thread itself (Java can have Thread.yield () to yield execution time, but no way to get execution time).
Although preemptive scheduling is done automatically by the system, we can give a "recommendation", that is, by setting a thread's precedence-priority .
The Java language has 10 levels of thread priority, in fact in code is 1-10 10 int Constants (default 5), setPriority(int x)
set by.
But do not rely on priority, because it is not reliable, the end result still depends on the OS. For example, if Java has 10 priorities, and Windows provides only 7, how can they correspond to each other?
Therefore, do not rely too much on the priority level.
Note that preemptive scheduling is generally determined by the OS kernel, and the program can only use "imperative statements", even if the CPU is discarded.
For example, Thread.yield(int x);
in that way:
A hint to the scheduler, the current thread was willing to yield it current use of a processor. The scheduler is a free-to-ignore this hint.
4. Thread state and Body conversion
1. The Java language Specification defines the following 6 thread states:
New-Newly created
Threads that have not been started after they are created
Runable-Can be run
- The thread that is executing in the JVM is in this state (note that it is not executed in the CPU, see 2)
- The runable corresponds to two OS thread states running andready, which means that the Java thread of the runable state may be executing or is waiting for the CPU to allocate execution time to it.
Blocked-Blocked
The lock of the monitor that the other thread occupies, which waits for the thread to enter the synchronization area (waiting to acquire the lock), which is in this state
Waiting-Wait indefinitely
In the wait set of the object that the monitor belongs to, waiting for the other (this object monitor owner) thread to wake up. Call the following method when the current thread enters the waiting state:
- Obj.wait ()/obj.wait (0) method
- Threadobj.join ()/threadobj.join (0) method
- Locksupport.park () method
Timed waiting-term waiting
The system will automatically wake up within a certain period of time, and of course it will be awakened. Calling the following method causes the thread to enter the age-waiting state:
- Threadobj.sleep (N) method
- Threadobj.join (N) method
- Locksupport.parknanos (obj, N) method
- Locksupport.parkuntil (obj, N) method
Terminated
A thread that has been terminated is in this state
2. State transitions are as follows
It is important to note that these States are not necessarily accessible through threading Thread.getState();
methods. Rather, a waiting in the process it certainly does not make the getState()
action.
Java thread and thread synchronization-threads (2)
Java threading Model, thread state-Threads (3)
Reference documents:
[1] Zhou Zhiming. Deep understanding of Java Virtual Machines [M]. 2nd edition. Beijing: Mechanical industry Press, 2015.8.
[2] James gosling,bill joy,guy steele,gilad bracha,alex buckley.the Java? Language specification. Java SE 8 Edition. English version [eb/ol].2015-02-13.
Java threading Model, thread state-threads (1)