Thread state of life

State of the thread conversion:

 

 

 

1, a new state
after the establishment of a thread object using new Thread (), the thread object is in a nascent state.


2, ready
to enter the ready state (runnable) method by start calling thread. Note: You can not thread has been started calling start () method again, otherwise there will be Java.lang.IllegalThreadStateException exception.
Thread in the ready state already has a running condition, but has not been assigned to the CPU, the thread is in the ready queue (ready pool), wait for the system to assign CPU.
Note: If you want the child thread calls start () method to perform immediately, you can use Thread.sleep () mode so that the main thread to sleep for a Huoer, turn to execute child thread.


3, the operating state
of threads running the most complex, it can become blocked state, ready state, and the state of death.
Thread in the ready state, if obtained cpu scheduling, will run from the ready state to state, execution run () method in the task.
If the thread is lost cpu resources, it will run and from the state to the ready state, waiting to re-allocate resources system.
You can also call the state of threads running yield () method, it will make the cpu resources, become ready again.


4, blocking state
when the following happens, the thread will make the control of the CPU and temporarily stop their run, blocked from running state to state:
① thread calls sleep method to release CPU actively control
② IO thread calls a blocking method before the method returns, the thread is blocked
③ threads try to obtain a synchronized monitor, but change the synchronization monitor is being held by another thread
④ thread waits for a notification (notify) in
⑤ program calls the suspend method thread the thread is suspended. However, this method is easy to cause a deadlock, so the program should try to avoid using this method.
Thread blocking state can not enter the ready queue. Only when the cause of obstruction of elimination, such as sleep time is up, or wait for the I / O device idle down, the thread into the ready state, again into the ready queue waiting in line, after being selected from the original system stopped position continue to run.
When the following happens, the thread will be blocked from running state to state:


5, the state of death
when the thread is run () method executed, or is forcibly terminated, such as abnormal, or by calling the stop (), desyory () method, etc., will be transformed from a running state to the state of death.
Upon the death of threads, it is final. If you call start on a dead thread () method throws an exception java.lang.IllegalThreadStateException.

 

Note:

1. sleep () is a static method of the Thread, best not to call it a Thread instance of an object, because it is always sleep the thread currently running, instead of calling it the thread object, it only thread running state object is valid.

 

2. Java thread scheduling is a core multi-threaded Java, only good schedule, in order to maximize the performance of the system and improve the performance of programs.

But no matter how programmers write scheduling, can only maximize the impact of threads of execution order, but can not do precise control .
After using sleep method, the thread is blocked to enter the state, and only when sleep time has elapsed, will re-enter the ready state, and ready to run into the state, is controlled by the system, we can not precisely to interfere with it, so if you call Thread.sleep (1000) makes the thread to sleep for one second, the result may be greater than 1 second.

 

3. yield () method

yield () method, and sleep () method is somewhat similar, is also a static method of the Thread class provides, it also allows the currently executing thread pause and let the cpu resources to other threads.
However, and sleep () method is different is that it does not enter the blocked state, but into the ready state.

 

4. join () method, thread consolidation
merged thread meaning that several parallel threads in a thread consolidated into a single-threaded execution scenario is when a thread must wait for another thread to finished executing, Thread class provides join method to accomplish this function, note that it is not a static method.
Can be seen from the above list of methods, it has three overloaded method:
void the Join ()    
    current thread to wait added behind the thread, the thread waits for termination. That is, wait for the thread is finished after the execution of the current thread.    
void join (long millis)    
    current thread to wait for this thread to a maximum of millis milliseconds. If, within millis time, the thread execution is not complete, then the current thread into the ready state and waits cpu scheduling   
void join (long millis, int nanos     )
    wait time for this thread to most millis milliseconds + nanos nanoseconds. If, within millis time, the thread is not executed, then the current thread into the ready state and waits cpu scheduling  

 

5.  daemon thread

Daemon thread common thread with the wording of fundamental differences on what it calls a thread object method setDaemon (true), you can set it up as a daemon thread.

setDaemon detailed description of the method:
public void setDaemon Final (Boolean ON) the thread is marked as a daemon thread or a user thread. When running thread is a daemon thread, Java virtual machine exit. This method must be called before starting the thread. 

In fact: the JRE determine whether the program execution end of the standard is all foreground threads executing the line finished, regardless of the state of the background thread, so using a background thread (daemon thread) must pay attention to this issue.

Benefits Guardian line is that you do not care about the end of it the problem. For example, when you want your applications running play background music, if the thread that play background music set to a non-threaded daemon, then quit when the user requests,

Not only to withdraw from the main thread, the thread also notice the background music of exit; if you do not need to set up a daemon thread.

 

6.  How to end a thread

Methods Thread.stop (), Thread.suspend, Thread.resume, Runtime.runFinalizersOnExit termination of threads running these have been abandoned, is extremely unsafe to use them!

We want to end a thread safe and effective, as long as under certain circumstances, run method can be executed to completion. Instead of while (true) wireless cycle.

Sometimes the thread run () method does not normally finished (may turn into blocked at run time), interrupt can make use of the Thread object in this case () method will interrupt status is set to true end of a thread.

When a thread is in sleep, wait, one of three states join, and if this time he's interrupted status is true, then it will throw an exception InterruptedException, and interrupt status is reset to false.

 

We take a look at the statement sleep, wait, join methods:

public final void wait() throws InterruptedException  
public static native void sleep(long millis) throws InterruptedException public final void join() throws InterruptedException 

You can see, these three have in common are a InterruptedException exception is thrown in. So, we can use interrupt this clever way off the end of the thread in this case.

Guess you like

Origin www.cnblogs.com/lcj12121/p/11370282.html