Required for thread interview: thread status and dump output status, draw at the end of the article!

Get technical dry goods and industry information for the first time!

Required for thread interview: thread status and dump output status, draw at the end of the article!

☞ Free CSDN information to help service | Free group membership ☜

When interviewing Java, threading is definitely an indispensable knowledge point. Therefore, learning it is definitely a must, and you must not wait to learn it until you encounter it. It will be too late.

Generally define a thread, there are 6 states.

Required for thread interview: thread status and dump output status, draw at the end of the article!

The 6 states are explained below.

new represents the new state; RUNNABLE running state, ready (ready) and running (running) two states are collectively called "running"; BLOCKED blocked state, the thread is blocked in the lock; WAITING waiting state, the thread that enters this state needs to wait Other threads make some specific actions (notification or interrupt); TIMED_WAITING timeout waiting state, which is different from WAITING, which can return on its own after a specified time; TERMINATED termination state, which means that the thread has been executed.

Required for thread interview: thread status and dump output status, draw at the end of the article!

If you still don’t understand, you can read my article "Understanding multithreading, first understand the basic state of threads from the "graph"! ".

These 6 states seem to be easy to understand, but in actual work, when an exception occurs in the program, you will find that the state in the stack is different from the state above.

In the dump file, the various thread states are explained as follows:

  • Deadlock, Deadlock (focus on)

  • In execution, Runnable

  • Waiting for resources, Waiting on condition (focus on)

  • Waiting to get the monitor, Waiting on monitor entry (focus on)

  • Object waiting, Object.wait() or TIMED_WAITING

  • Suspended

  • Blocked (focus on)

  • Stop, Parked

Regarding the key concerns mentioned above, let me briefly introduce them.

Deadlock state

Required for thread interview: thread status and dump output status, draw at the end of the article!

This is a typical deadlock stack. The t1 thread is locked at address 0x22a297a8, while the t2 thread is waiting to lock at this address. What's more interesting is that the stack also records the number of lines of code where the deadlock occurred, which is a great help for us in locating the problem.

Waiting on condition

The most common situation is that the thread is sleeping, and it will be awakened when the waiting time for sleep is up. Keywords: TIMED_WAITING, sleeping, parking. TIMED_WAITING may be caused by calling wait with a timeout parameter. Parking means that the thread is suspended.

Required for thread interview: thread status and dump output status, draw at the end of the article!

If the stack information is clearly application code, it proves that the thread is waiting for resources. Generally, when a large amount of a resource is read, and the resource lock is used for the resource, the thread enters the waiting state and waits for the resource to be read.

Required for thread interview: thread status and dump output status, draw at the end of the article!

Waiting on monitor entry

Means that the thread is waiting to enter a critical section. Monitor is the main method used to achieve mutual exclusion and cooperation between threads in Java. It can be seen as a lock of an object or a class. Every object has, and only one monitor.

This state usually occurs when the thread is waiting for the database connection pool to return an available connection.

Required for thread interview: thread status and dump output status, draw at the end of the article!

Blocked

Thread blocking refers to a thread that has been waiting for a long time but has not been able to obtain the required resources during the execution of the current thread. The thread manager of the container identifies it as a blocked state, which can be understood as a thread waiting for the resource to time out. If the thread is in the Blocked state, but the reason is not clear. You can use jstack -m pid to get the mixed information of the thread.

Required for thread interview: thread status and dump output status, draw at the end of the article!

For example, the above information indicates that the thread is blocked while trying to enter a synchronized block.

The above content, I like to help everyone in the interview. Finally, I want to emphasize.

When the program fails, the information of a dump is often not enough to confirm the problem. It is recommended to generate dump information three times. If each dump points to the same problem, we can determine the typicality of the problem.

The stack information is just a reference. Some normal running threads are also problematic due to the complex network environment and IO. They cannot be located with jstack and need to be combined with the understanding of the business code.

Required for thread interview: thread status and dump output status, draw at the end of the article!

Above is an event draw, a mouse pad! The prize will be mailed next week! The probability of forwarding the prize is higher!

Guess you like

Origin blog.51cto.com/15127565/2666936