|
The concept of process, multi-process, thread, multi-thread
Process (process): execution path of the CPU. Popular to say that the system is running the program. For example, we open a browser, QQ and so on, these programs run once opened, the so-called process.
Multi-process: the system running multiple programs simultaneously. We should not be difficult to understand this, open the browser and we can also QQ chat, CS stand-alone games.
Thread (thread): In the process of running the operation unit. Such as Thunder download one of our tasks is to download a thread.
Multithreading: Similarly we can see, each of which has multiple independent process or have mutual cooperation relations run unit which we call multi-threading.
The first multi-threaded program
Java multi-threaded implementation in two ways: to inherit the parent class Thread achieve Runnable interface.
First, we implement a simple multi-threaded alternately through the program and outputs 1 to 20.
package MyJavaThread;
public class MyFirstThreadTest {
public static void main (String [] args) {
new Thread (new Runnable () {
public void run () {
int i = 1;
while (i <= 20) {
System.out.println (Thread.currentThread () getName () + ":" + i ++.);
}
}
.}) Start ();
int i = 20;
while (i> 0) {
System.out.println (Thread.currentThread () getName () + ":" + i--.);
}
}
}
There is no real sense of multithreading
We know, CPU can only be assigned to a program at the same time resource (monocytes case), which is given the power to run a program, then we can see that once the program is actually run several CPU implementation of the right to switch back and forth to produce results. Thus make people feel that run concurrently, but very fast switching speed CPU Bale.
State of the thread
Thread has its own state, we call the life cycle: the initial state, operating status, freezing and final states.
Thread sunrise - initialization state
Initialize the thread of what we call a thread is created, which is an instance of a subclass of Thread, waiting to be start.
Thread youth - run state
When we call the method start time, when the thread is in the running state. But rigorous said at this time is not necessarily the thread will run immediately, it is a temporary state, namely the implementation of the queue among the CPU, CPU wait for execution reincarnation, get the execution right.
Thread cold - frozen
After the thread is called sleep method or by calling the wait method, give up the implementation of the CPU. However, after the freeze can return to running again, reacquire the execution right of the CPU. Of course, you can also interrupt or exception occurs when the direct-to-death state.
Thread ending - dead state
If the thread execution logic appeared fatal exception or the thread is completed, which means that the thread is to say byebye curtain call. Thread after death can not return to any state.
to sum up
A process has at least one thread running
Main function itself is a thread, we called the main thread
Multi-threaded approach can be inherited Thread class, you can also implement Runnable
No concurrent strict sense
JVM itself has a lot of background threads running, supporting our program
Initialization state only to the operating state
Status can run into a frozen state and state death
Can a frozen state to a running state and a state of death
The state of death can only accept the fact of death |
|
|
|