Thread Status and conversions
Borrow a picture from the Internet:
Description
Threads are divided into 5 different states
New State (new)
When the thread object is created, it enters the new state, for example: thread t = new thread ();
Ready state (Runnable)
When the thread object is created, the other thread invokes the object's start () method to start the thread.
For example, Thread.Start (). The thread is in a ready state, and then waits for CPU scheduling to execute. Not the Run () method-_-;
When start () is called multiple times, an exception is reported.
publicsynchronizedvoidstart() { //A zero status value corresponds to state "NEW". if0) thrownew IllegalThreadStateException();
Run (Running)
Threads get CPU permissions to execute. It is important to note that a thread can only go from a ready state to a running state.
Clogging (blocked)
A blocking state is a time when a thread abandons the CPU for some reason and temporarily stops running. Until the thread is in a ready state, the opportunity to go to the running state is reached. There are three types of blocking, with two states (blocked and waiting):
- Wait for blocking--by calling the thread's Wait () method, to let the thread wait for the completion of a job.
- Synchronous blocking--a thread that acquires a synchronized synchronization lock fails (because the lock is occupied by another thread) and it goes into a synchronous blocking state.
- Other blocking-the thread goes into a blocking state by calling the thread's sleep () or join () or making an I/O request. 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.
Blocked and waiting differences, blocked is outside the critical point of waiting to enter, waiting is in the critical point to wait for notify, when the thread calls the Join method, jion the other thread, will also enter the waiting state, Wait for the thread that is jion by it to end.
Death (Dead)
The thread finishes executing or exits the run () method because of an exception, and the thread ends the life cycle.
Creation of Threads
Threads are created in 3 ways:
1, Inherit Thrad:
new Thread(){ publicrun(){ System.out.println("new Thread"); };};
2. Implement Runnable Interface:
new Thread(new Runnable(){ publicvoidrun(){ System.out.println("new Thread"); };});
3. Create threads using callable and future
new futureTask<String>(){ publiccallthrows Exception{ System.out.println("new Thread"); return"abc"; new Thread(ft);ft.start();String result= ft.get();
Method in the Threading method object
Wait (), notify (), Notifyall () are all defined in the object class, they all depend on the synchronous lock, and the synchronous lock is the object lock held, and each object has only one! This is why notify (), wait () functions are defined in the object class, not in the thread class.
Note: Call Wait/notify/notifyall must be in the acquisition of the object's lock, otherwise it will be an error ha! Java.lang.IllegalMonitorStateException
Wait ()
The method is to put the current thread into a wait (waiting) state, and also let the current thread release the lock it holds (a synchronous lock) until another thread calls the Notify () live Notifyall () of the object, wakes the thread (single or all) on the current object, and lets it go into a ready state. Wait for the CPU to dispatch before you can continue to execute wait code;
Wait (long timeout)
It also allows the current thread to enter the Wait (timed_waiting) state, which can specify the time-out period, when the Notify () or Notifyall () of this object is called, or the thread will not wake up and go into a ready state if the specified time is exceeded;
Wait (long Timeout,int naous)
Notify () and Notifyall () are waking up the waiting thread on the current object, notify is waking up a single thread, and Notifyall is waking up all the threads;
The whole process is this: when a thread acquires a synchronization lock on an object and then executes until the wait () method, the thread enters the wait state, releasing the synchronization lock for that object, when another thread (the wakeup thread) acquires the synchronization lock (the synchronization lock must be the same as the waiting thread's sync lock). After executing to notify () or Notifyall (), the wait thread can be woken up, although the waiting thread is awakened, but it cannot be executed immediately because the wake-up thread also holds the synchronization lock on the object, and must wait for the wake thread to release the object's synchronization lock. Wait for the thread to get the object's synchronization lock and continue running.
Method in Thread
Yeild (), sleep () is defined in the thread class
Yeild ()
Its role is to compromise, that is, the running state into the ready state, so that other threads with the same priority to get execution, but there is no guarantee that after the current thread calls the Yeild method, the other thread must be able to obtain the execution, it is possible that the current thread again into the running state to continue execution.
Sleep ()
Let the current thread hibernate, that is, the current thread from the running state to the blocking state, sleep can specify the sleep time, when the thread's sleep time exceeds the specified time, the thread will be awakened, the blocking state becomes ready to install, and then wait for CPU scheduling.
It is important to note thatsleep () is a synchronous lock that does not release the object , simply blocking the thread.
Interrupt ()
When a thread calls interrupt (), just sets the interrupt state of the thread, does not terminate the thread, the rest is handled by yourself, wait (), wait (timeout), join (), sleep (timeout), await (), Methods such as await (timeout) can be interrupted by the interrupt () method.
The thread frees the lock by executing the method, calling wait, and throwing the code out of the exception.
java-Threads-Basics