There are several states in the Java Thread's run cycle, which are defined and explained in detail in Java.lang.Thread.State:
NEW: The state of a thread that has not been started so far.
RUNNABLE: The thread state of the thread that can be run. A thread in a running state is running in a Java virtual machine, but it may be waiting for other resources in the operating system, such as a processor.
BLOCKED: The thread State of one of the threads that is blocked and waiting for a monitor lock. A thread in a blocked state is waiting for a monitor lock to enter a synchronized block/method, or to call theObject.waitThen enter the synchronized block/method again.
waiting: The waiting thread is waiting for another thread to perform a specific operation. For example, a thread that has already called object.wait () on an object is waiting for another thread to call object.notify () or object.notifyall ()on that object. A thread that has already called Thread.Join () is waiting for the specified thread to terminate. [Thread.Sleep (), object.wait () without time-out setting, Thread.Join () with no time-out value)]
timed_waiting: The thread state of a waiting thread with a specified wait time. A thread is in a timed wait state because it calls one of the following methods with the specified positive wait time: [Thread.Sleep (), object.wait () with time-out setting (), Thread.Join () of the generation timeout value)
TERMINATED: The thread state of the thread has been terminated. The thread has finished executing.
Let's talk about how to get threads into the above states: 1.NEW, this is the simplest.
static void NEW () { Thread t = new Thread (); System. Out.println (T.getstate ()); }
Output NEW2.RUNNABLE, also simple, let a thread start, while the code inside do not sleep or wait wait, etc.PrivateStaticvoidRUNNABLE () {Thread T =NewThread () { PublicvoidRun () { for(inti=0; I<integer.Max_value; i++) {System. out. println (i); } } }; T.start (); } 3.BLOCKED, this must be at least two threads above, and then wait for each other synchronized blockPrivateStaticvoidBLOCKED () {FinalObject lock =NewObject (); Runnable Run =NewRunnable () {@Override PublicvoidRun () { for(inti=0; I<integer.Max_value; i++) {synchronized(lock) {System. out. println (i); } } } }; Thread T1 =NewThread (run); T1.setname ("T1″"); Thread t2 =NewThread (run); T2.setname ("T2″"); T1.start (); T2.start (); At this time, one in runnable, the other in blocked (waiting for another thread of System.out.println: This is an IO operation, belongs to system resources, does not cause waiting, etc.) 4.Waiting, this needs to be used in the producer consumer model, and when producers are slow, consumers wait for the next notify of producersPrivateStaticvoidWaiting () {FinalObject lock =NewObject (); Thread T1 =NewThread () {@Override PublicvoidRun () {inti = 0; while(true){synchronized(lock) {Try{lock.wait (); }Catch(Interruptedexception e) {} System. out. println (i++); } } } }; Thread t2 =NewThread () {@Override PublicvoidRun () { while(true){synchronized(lock) { for(inti = 0; i< 10000000; i++) {System. out. println (i); } Lock.notifyall (); } } } }; t1.setname ( "^^ t1^^"); t2.setname ( "^^t2^^ "); t1.start (); t2.start (); } 5. timed_waiting, this only needs to be on the basis of 4, the Wait method plus a time parameter to limit is OK. Change the synchronized block in 4 to the following.synchronized(lock) {Try{lock.wait (1000L); }Catch(Interruptedexception e) {} System. out. println (i++); Another look at the output of the stack, called timed_waiting (on object monitor), shows that there are other things behind the brackets, such as sleep, and we'll just change the T2 for loop to sleep:synchronized(lock) {
Try{Sleep(30*1000l); }Catch(Interruptedexception e) {} lock.notifyall ();} See, T2 's state is timed_waiting (sleeping), and T1 is still on object monitor, because T1 or wait is waiting for T2 notify, and T2 is itself sleep in addition, the join operation is also entered O N Object Monitor 6.TERMINATED, this state, as long as the thread ends the Run method, it will enter ...PrivateStaticvoidTERMINATED () {Thread T1 =NewThread (); T1.start (); System. out. println (T1.getstate ());Try{Thread.Sleep(1000L); }Catch(Interruptedexception e) {} System. out. println (T1.getstate ()); } output: Runnableterminated because the thread's Start method is started asynchronously, it is possible to get the state immediately after it executes, and it is not done yet. Well, in a word, the potential performance bottleneck in the system is found to be useful. When the Java system is running slowly, we should first find the bottleneck of performance, andJstackTools such as the current stack of the JVM can see the state of all threads of the current entire VM, when we see a thread state is often in waiting or blocked, be careful, he may be waiting for resources are often not released (of course, Thread pool scheduling is also a variety of queue locks, to distinguish between, for example, this is a classic concurrent package inside the thread pool, its scheduling queue is Linkedblockingqueue, the execution takes when the block, waiting for the next task into the queue, and then into the execution, This theory is not a system of performance bottlenecks, to find bottlenecks in generalFind your own code first.Stackand then we'll check out those open source components.Some ideas on/JDK problem: 0. How do I track a thread? See above the stack output is not, the first line is the content is threadname priority tid nid desc more than tracking tid, nid can only find the thread. 1. The discovery of a wired path into the block, and lasted for a long time, this indicates that the performance bottleneck exists in the synchronized block, because he has been block live, can't get in, the other thread has not been processed, and this synchronized block processing speed is relatively slow, And then look deeper. Of course, it is also possible to block too many threads at the same time, queuing too long caused. 2. Discover that the thread has entered the waiting for a long time, indicating that the performance bottleneck exists in the logic that triggered the notify. Of course, there are too many threads waiting at the same time, always waiting to be released. 3. Thread enters time_waiting state and lasts for a long time, same as 2 troubleshooting. The above black and white are printed through jstack, you can directly navigate to the thread you want to know the execution stack, which has a great effect on the Java performance bottleneck analysis.
Status of Threads in Java