Java The status of threads in is divided into 6 Kind of .
1. initial (NEW)： A new thread object is created , But it's not called yet start() Method .
2. function (RUNNABLE)：Java The thread will be ready （ready） And in the operation of the （running） The two states are generally referred to as “ function ”. After the thread object is created , Other threads ( such as main Threads ） Called the object's start() Method . The thread in this state is in the runnable thread pool , Waiting to be selected by thread scheduling , obtain CPU Right to use , It's in a ready state （ready）. The thread in the ready state is getting CPU After the time slice, it becomes the running state （running）.
3. Blocking (BLOCKED)： Indicates that the thread is blocked by a lock .
4. wait for (WAITING)： A thread entering this state needs to wait for some specific action from another thread （ Notification or interruption ）.
5. Overtime waiting (TIMED_WAITING)： This state is different from WAITING, It can return on its own after a specified time .
6. End (TERMINATED)： Indicates that the thread has finished executing .
State diagram of thread
1. The initial state
Realization Runnable Interfaces and inheritance Thread You can get a thread class ,new An example comes out , The thread is in its initial state .
2.1. Ready state
Ready state just means that you are qualified to run , The scheduler didn't pick you , You are always ready . Calling thread's start() Method , This thread is in ready state . Current thread sleep() Method end , Other threads join() end , Wait for user input to complete , A thread gets the object lock , These threads will also be ready . The current thread time slice is used up , Call the... Of the current thread yield() Method , The current thread is in ready state . After the thread in the lock pool gets the object lock , Get ready .
2.2. In-service state
The thread scheduler selects a thread from the runable pool as the current thread's state . This is also the only way for a thread to enter the running state .
3. Blocked state
The blocking state is that the thread is blocking into synchronized Keyword decorated method or code block ( Get the lock ) State of .
4. wait for
Threads in this state are not allocated CPU execution time , They wait to be awakened explicitly , Otherwise it will be in a state of waiting indefinitely .
5. Overtime waiting
Threads in this state are not allocated CPU execution time , But there is no need to wait indefinitely for other threads to wake up , They wake up automatically after a certain time .
6. Termination status
Thread run() When the method is complete , Or the main thread 's main() When the method is complete , We think it's over . This thread object may be alive , however , It's not a single thread anymore . Once the thread is terminated , Can't be reborn . Called on a terminated thread start() Method , Will throw out java.lang.IllegalThreadStateException abnormal .
7. Waiting in line
call obj Of wait(), notify() Before the method , Must obtain obj lock , That is to say, it must be written in synchronized(obj) In the code snippet . Steps and figures related to waiting queue
Threads 1 Get objects A Lock of , Using object A.
Threads 1 Call object A Of wait() Method .
Threads 1 Release object A Lock of , And immediately into the waiting line .
The object in the lock pool competes for the object A Lock of .
Threads 5 Get object A Lock of , Get into synchronized block , Use object A.
Threads 5 Call object A Of notifyAll() Method , Wake up all threads , All threads enter the synchronization queue . If a thread 5 Call object A Of notify() Method , Then wake up a thread , I don't know who will wake up , The awakened thread enters the synchronization queue .
notifyAll() The way is synchronized end , Threads 5 Release object A Lock of .
Threads competing for object lock in synchronous queue , But threads 1 I don't know when I can get it .
Synchronization queue status
The current thread wants to call the object A When synchronizing methods , Find out who A The lock of is owned by another thread , At this time, the current thread enters the synchronization queue . in short , All threads in the synchronization queue want to compete for the object lock .
When a thread 1 By another thread 2 Wake up ,1 The thread enters the synchronization queue , To fight for the lock . Synchronous queue is a concept only existed in synchronous environment , An object corresponds to a synchronization queue .
Comparison of several methods
Thread.sleep(long millis), This method must be called by the current thread , The current thread enters TIMED_WAITING state , But don't release the object lock ,millis After that, the thread automatically wakes up and enters the ready state . effect ： The best way to give other threads the chance to execute .
Thread.yield(), This method must be called by the current thread , The current thread gave up getting CPU Time slice , But don't release lock resources , From running state to ready state , Give Way OS Choose thread again . effect ： Let threads of the same priority take turns to execute , But there is no guarantee that it will take turns . In practice, there is no guarantee that yield() To achieve the purpose of concession , Because the compromised thread may be selected again by the thread scheduler .Thread.yield() It won't cause obstruction . The method and sleep() similar , It's just that the user can't specify how long to pause .
t.join()/t.join(long millis), Call other threads in the current thread t Of join Method , The current thread enters WAITING/TIMED_WAITING state , The current thread will not release the object lock already held . Threads t Done or millis Time out , The current thread is in ready state .
obj.wait(), The current thread calls the object's wait() Method , The current thread releases the object lock , Enter the waiting queue . rely on notify()/notifyAll() Wake up or wait(long timeout) timeout Time to wake up automatically .
obj.notify() Wake up a single thread waiting on this object monitor , Choice is arbitrary .notifyAll() Wakes up all threads waiting on this object monitor .