Processes and Threads
process : is a program that is being executed. Each process execution has an execution order, which is an execution path, or a control unit.
thread : is an independent control unit in the process.
The thread is controlling the execution of the process.
In a computer where multiple threads get the execution of the CPU, the CPU executes to who, who runs it, it is clear that, at some point, it can only be run by one program (except multicore), and the CPU is doing a quick switch to achieve what appears to be a colleague's running effect. We can image the multi-threaded running behavior in each other Rob CPU execution, this is a feature of multithreading: Randomness. Who grabs who executes, as to how long it takes to execute, the CPU decides.
Advantages of multithreading
Multi-thread can make the software run faster, such as thunder can be multiple threads simultaneously download a file, speeding up the download speed
L cannot share memory between processes, while threads can
How to create a startup thread:
Method One:
1. Define class inheritance Thread
2. The Run method in the thread class is replicated to store the custom code in the Run method to let the thread run
3. Call the thread's Start method (this method has two functions: 1. Start thread; 2. Call the Run method)
Method Two:
1. Define class implementation Runnable interface
2. Overwrite the Run method in the Runnable interface to store the code that the thread will run in the Run method
3. Thread objects are established through the thread class
4. Pass the subclass object of the running interface as the actual parameter to the constructor of the thread class.
5. Call the Start method of the thread class to open the thread and invoke the Run method of the Runnable interface subclass
why would you want to runnable the subclass object of the interface is passed to the thread the constructor?
Because the object that the custom run method belongs to is the subclass object of the Runnable interface, let the thread specify the run method of the specified object
Why do you want to overwrite run method?
The thread class is used to describe threads, and the class defines a feature that stores the code that the thread wants to run. This storage function is the Run method, which means that the run method in the thread class is used to store the code that the thread will run.
What are the differences between the two ways of implementation?
1, the implementation of runnable interface to avoid the limitations of single inheritance, in the definition of the thread is recommended to use the implementation of runnable, after the implementation of multithreading can also inherit other classes
2. The thread code of the inherited thread class is stored in the Run method of the thread subclass, which implements the runnable thread code stored in the Run method of the subclass of the interface.
The state of the thread
New and ready
When a new thread is created with the new keyword, the thread is in the new state, which, like any other Java object, simply allocates memory for it by the Java Virtual machine and initializes its value
After the start () method is called, the thread is in a ready state, indicating that the thread can run, but when it is running, it looks at the internal dispatch of the JVM.
Run and block
When the thread gets execution permission, the thread body inside the Run method starts executing, and the thread is running
The Sleep method allows the thread to freeze the time length, and then continue executing the thread
The wait () method can also freeze threads, and after freezing, the Notify () method is required to wake the thread, otherwise it will freeze
thread Death
The Run method internal code executes or encounters an exception or error when the thread ends and the thread enters the dead state
The Stop () method can kill a thread directly, but it is prone to deadlock and is usually not recommended
Temporary state (blocked state)
When multiple threads are running at the same time, and the CPU can only run one thread, the other threads first enter a temporary state (blocking state)
JAVA Multithreading (i)