One. Thread Status
1. New state: A new Thread object was created.
2. Ready state (Runnable): After the thread object is created, other threads call the object's start () method. The state of the thread is located in a pool of running threads that becomes operational and waits for the CPU to be used.
3. Running state (Running): The ready state of the thread gets the CPU and executes the program code.
4. Blocking state (Blocked): The blocking state is a temporary stop for a thread that has abandoned the CPU usage for some reason. Until the thread is in a ready state, the opportunity to go to the running state is reached. There are three types of blocking:
(i), waiting for blocking: The running thread executes the wait () method, and the JVM puts the thread into the waiting pool.
(ii), synchronous blocking: When a running thread acquires a synchronization lock on an object, the JVM puts the thread into the lock pool if the synchronization lock is occupied by another thread.
(iii), other blocking: The running thread executes the sleep () or join () method, or when an I/O request is made, the JVM will place the thread in a blocked state. When the sleep () state times out, join () waits for the thread to terminate or time out, or the I/O process finishes, the thread is re-entered in a ready state.
5. Death status (Dead): The thread finishes executing or exits the run () method because of an exception, and the thread ends the life cycle.
Two. Two ways to implement threading
1. Inherit the thread, after creation, implement the Start () method to launch the thread;
2. Implement runnable, and then call the new thread () method, and then call the start () method to start the thread.
Three. How threads are synchronized
Three kinds, synchronized, wait and notify three ways
Thread Run status record