Contrast with java thread Thread, chat about thread status

We know that the life cycle of a thread is divided into five life states: new, ready (runnable), running, waiting, and dead. These states are inevitably experienced by almost all threads. In an interview before, an interviewer asked me, how many states does the thread class Thread of java have? 5 kinds! The interviewer smiled. . I never had that interview anyway.

Although I felt that I answered no problem, I checked it when I came back, and said! The thread status of Thread is 6 types of TMD. At that time, there was a feeling that it was hit by dimensionality reduction. I didn't understand the meaning of the problem and I was KO directly! Later, I carefully compared the thread status, and I will talk about it here.

The life cycle of the thread:

1. New (new): Create a new thread object. Similar to new Thread();

2. Ready (RUNNABLE): After the thread creates the object, other threads call the start() method of the object, and the thread in this state is in the runnable thread pool, waiting to be selected by thread scheduling to obtain the right to use the CPU. After calling the start() method, it may not start, it depends on the allocation of thread resources.

3. RUNNING: The thread in the runnable state obtains the time slice of the CPU and executes the program code. To put it simply, the thread in the ready state obtains CPU resources and executes the method run(). At this time, the thread is in the running state. The thread in the running state may directly die (down), block (other threads join, this thread sleep, synchronized method, etc.), and become ready (wait+notify)

4. BLOCKED: The thread gave up the right to use the CPU, gave up the CPU time slice (timeslice), and temporarily stopped running. For example, after a thread executes sleep(), suspent (suspend) and other methods, after losing the occupied resources, the thread enters the blocking state from the running state. After that, the thread must enter the runnable state before it has the opportunity to obtain the CPU usage rights and enter the running state again. There are three blocking states:

            Waiting for blocking: The thread in the running state executes the wait() method, which causes the thread to enter the blocking state.

            Synchronous blocking: the thread fails to acquire the synchronized synchronization lock (other threads preempt the synchronized lock and occupy the monitor).

            Other states: call sleep() through this thread, or call join() by other threads, and issue I/O requests. The thread enters the blocking state. When sleep() timeout or join() method is executed or timed out, the thread is converted to ready again status.

5. DEAD: When a thread in a running state completes its task (the execution of the run() method ends) or terminates due to certain conditions, the thread switches to the dead state, and the thread's life cycle ends, and there will be no more state change.

Thread state flow diagram

 

Everyone may have seen the thread flow diagram similar to the above picture, so I won't talk about it here.

Let's take a look at the status of Thread in java.

1.NEW (New)

Is to inherit Thread or implement a runnable method to create a thread. At this point, the JVM allocates memory for it and initializes the value of the member variable. But the thread object does not show any dynamic characteristics of the thread, and the program does not execute the thread execution body of the thread.

2.RUNNABLE

Thread state can run the thread, the state can run a thread, which means you can run a thread has been running in the JVM , but there may be waiting for other system resources, such as processor. At this time, the JVM will create a method call stack and a program counter for the thread.

3.BLOCKED

In some cases, the thread in the running state gives up the CPU and temporarily stops its operation, and enters the blocking state. For example, when a thread initiates a blocking I/O, or applies for a resource already occupied by other threads, the thread will be in this state. The thread in the BLOCKED state will not occupy the processor resources. When the blocking I/O operation is completed or the resource is requested, the thread will switch to the RUNNABLE state.

4.WAITING

After a thread has executed some specific methods, it will be in this state of waiting for other threads to perform operations. For example, execute Object.wait(), join other threads, or LockSupport.park(Object).

5.TIMED_WAITING

This state is similar to WAITING, the difference is that the thread in this state is not unlimited waiting for other threads to perform a specific operation to wake up, but in a time-limited waiting state. When other threads do not perform a specific operation expected by the thread within a specific time, the thread automatically switches to the RUNNABLE state.

6.TEMINATED

Threads that have finished executing are in this state. Since a thread instance can only be started once, a thread can only be in this state once. Normal return of Thread.run() or early termination due to throwing an exception will cause the thread to be in this state.

A very classic picture in the art of java programming

 

Let's take a look at this picture, and understand all of these, the conversion between threads should be no big problem, O(∩_∩)O~

1.Thread.start(), a thread is started after it is created, and the start method is called, which is equivalent to a new thread calling the run method. If the run method is called directly, it is called by this thread.

2.yield(). Thread calls this method. For Thread, the state is still RUNNABLE, but time slice resources are given up. Once the system schedules to obtain resources, the thread will continue to execute. Comparing the five states in the thread, calling this method is to make the thread enter the ready state.

3.Object.wait(), let’s talk about it here, wait() is a built-in method of the Object class. If you are interested, you can look at Object, which is helpful to understand java objects. The thread calls the wait() method. If the time is not set, the Thread enters the WAITING state, and if the time is set, it enters the TIMED_WAITING state. This corresponds to the state of the thread. It should be blocked and can no longer be running.

4.Object.join everyone to join are not very familiar with the case can go before I wrote to talk about the understanding of java threads (two) -------- key join . For example, if a main() method is being executed and a thread Thread.join() comes in, the thread of the current main() method will wait until the thread that joins in is executed.

5. LockSupport.part(thread) thread waits, only execute LockSupport.unPark(Thread) and allow the thread to execute, will it continue to execute.

6. Object.notify wakes up the waiting thread. At this time, the thread becomes RUNNABLE again, waiting for CUP scheduling to obtain resources.

7. Object.notifyAll() wakes up all waiting threads, similar to notify.

8. LockSupport.unpark(Thread) is allowed to run, and permission is to release the waiting of LockSupport.part(thread). This is locked in ReentrantLock, and is used in releasing the lock. If you are interested, you can check it out.

9.Thread.sleep(long), this is to directly enter the TIMED_WAITING state, but it should be noted that the sleep() method does not let out the control of the occupied resources, but keeps the thread waiting there, if it is wait(long) If it is, it will give up.

10.Object.wait(long), also the state enters TIMED_WAITING.

11.LockSupport.parkNanos(), LockSupport.parkUtil() These two methods also directly enter the TIMED_WAITING state, but I haven't seen it much, and interested partners can go and learn.

12. The synchronized method, waiting to acquire the monitor lock, the thread is blocked, after acquiring the lock, the thread re-enters the RUNNABLE state.

All threads and concurrent processing in java are inseparable from the control of thread state. Let's talk about it today. If you have time, you can try to write a thread state transition test method. Interested partners can try it.

No sacrifice,no victory~

 

Guess you like

Origin blog.csdn.net/zsah2011/article/details/109305867