Processes and Threads
When it comes to multithreading, you have to first talk about the concepts of processes and threads.
Process
A process can be understood as a basic unit of operation managed by the operating system. 360 browser is a process, WPS is also a process, is running in the operating system ". exe" can be understood as a process
Thread
A child task that runs independently in a process is a thread. There are a lot of subtasks running like QQ.exe, such as chat threads, buddy video threads, download file threads, and so on.
How threads are Created
There are two ways of creating a thread:
1. Inherit the thread, overriding the run () method of the parent class.
Public class extends thread{ publicvoid run () { for (int i = 0; I < 5; i++) { + "in Run!" ); } }}
Public Static void Main (string[] args) { new MyThread00 (); Mt0.start (); for (int i = 0; i < 5; i++) { + "is running! "); }}
Look at the results of the operation:
Main is running! Thread-0 is running! Main is running! Thread-0 is running! Main is running! Thread-0 is running! Main is running! Thread-0 is running! Thread-0 is running! Main is running!
It is obvious that the main thread and the Thread-0 thread are running alternately.
It is also normal that some people may not see such a noticeable effect. The so-called multi-threading refers to the two-thread code that can run at the same time without having to wait for the code in another thread to run. For a single-core CPU, there is no real multi-threaded, at each point in time, the CPU will execute a specific code, because the CPU code time to execute quickly, so the code of two threads to perform the execution looks like the same execution at the same time. It is related to the time-sharing mechanism that how long it takes to execute a particular code. CTSS divides the CPU time into several time slices, the operating system takes the time slice as unit piece as the unit each thread's code, the better CPU divides the time slice the smaller. So not see the obvious effect is also very normal, a thread printing 5 words would have been very fast, it may be in the time slice of the completion of the execution. Therefore, the simplest solution is to increase the value of the for loop by a bit (you can also add the Thread.Sleep method to the for loop, after that).
2, realize runnable interface. is similar to inheriting from the thread class, but after implementing runnable, it is still going to start with a thread:
public class MyThread01 implements runnable{ public Span style= "color: #0000ff;" >void run () { for (int i = 0; I < 5; I++ + "Running!" ); } }}
Public Static void Main (string[] args) { new MyThread01 (); New Thread (mt0); T.start (); for (int i = 0; i < 5; i++) { + "is running! "); }}
The effect is also obvious:
Main is running! Thread-0 is running! Main is running! Thread-0 is running! Main is running! Thread-0 is running! Main is running! Thread-0 is running! Main is running! Thread-0 is running!
Comparison of two ways to implement multithreading
Take a look at the API for the thread class:
In fact, the thread class is also the implementation of the Runnable interface. The key to the comparison between the two implementations is the contrast between extends and implements, and of course the latter is good. Because the first, inheritance can only but inherit, implementation may be more implementation, second, the way of implementation of comparison of inheritance, but also to reduce the coupling between programs.
Therefore, the implementation of multithreading is almost always the way of using the Runnable interface. However, the following article, for simplicity, inherits the way of the thread class.
Thread state
There are six thread states in the virtual machine, defined in Thread.state:
1. Newly created state new
New but not the state of the thread that started. For example, "thread t = new thread ()", and T is a thread in the new state
2. Operational status runnable
New comes out of the thread, and the call Start () method is in the runnable state. A thread in the runnable state may be running in a Java virtual machine, or it may be waiting for the processor's resources, because a thread must obtain the CPU's resources before it can run the contents of its run () method, otherwise queued
3, blocking blocked
If a thread is waiting for a monitor lock in order to enter a synchronized block/method, then the state of the threads is blocking blocked
4, Waiting for waiting
A thread is waiting for the waiting state because it calls the wait () method of the object without the timeout, the join () method of the thread without the timeout, and the Locksupport Park () method.
5, timeout waiting timed_waiting
A thread is called by the Wait () method with the object that specifies the waiting time, the join () method of the thread, the sleep () method of the thread, the Parknanos () method of the Locksupport, Locksupport Parkuntil () method, it will be in the timeout wait timed_waiting state
6. Termination status Terminated
When a thread call terminates or the run () method finishes executing, the thread is in a signaled state. The thread in the terminating state does not have the ability to continue running
Java Multithreading 1: Overview of processes and Threads