Ride the wind and waves will sometimes, directly hang yunfan ji sea, the college entrance examination students come on!

Interviewer: Aren’t you proficient in Java concurrency? Let’s start with the basic Java thread lifecycle.

Ok, interviewer. Blah, blah, blah…

If we’re talking about the Java thread lifecycle, I think we should start with the operating system thread lifecycle

Since the JVM runs on top of the operating system, it is inescapable, and it can be said that threads in the Java language are essentially threads of the operating system

You are smart enough to realize that no operating system, Java or C# has a thread concept. Between them, there must be something in common about the life cycle of the thread, otherwise, the operating system has its own life cycle process, Java has its own set, C# has its own set, and can cooperate with each other, this kind of cost is too big to think about, right

So let’s take a look at what a common thread life cycle is

Let’s go straight to the picture

 

There are five terminated states: new, ready, running, waiting, and terminated

Among them:

  • New simply says that the thread is created, but not yet allowed to allocate CPU execution. Since this state only means that you are created at the programming language level, you are not created at the operating system level, so there is no way to allocate CPU execution

  • The ready state means that it has been successfully created at the operating system level, so the next step is to wait for the CPU allocation to be executed. Remember that classic line? Ready? Go!

  • Running state, I believe you will know, I have already ready, if you allocate CPU to me now, can I go? That’s running

  • Waiting is when the thread is in the running state and suddenly finds that, hey, I need to do some I/O or I need to wait for some event to happen (for example, I need some condition variable), it can’t continue happy running. To do that? Waiting the bai

    • Ll: I’m waiting. I need to free up the CPU. So a thread in waiting never gets a chance to use the CPU
    • If the words “never” terrify you, I’m not going to never get a chance to execute. Ll: I’m waiting. You can continue to be in the running state when the waiting event happens
  • When the thread terminated or an exception occurs, it is in terminated state. The thread is terminated and cannot move to any other state

The common thread life cycle and how they switch from one to the other should be clear by now, right

Let’s take a look at the Java thread lifecycle, and how it’s optimized on this basis. What’s the difference

The life cycle of Java threads

Let’s take a look at the state of the source definition (I’ve removed all the comments for emphasis) :

 
Copy the code

As you can see, there are six thread states defined in the source code. What are the common states? Five, right? Now it’s six.

What are these six? So I’m clear about these five states and how they switch between them, how do these six states switch between them?

Don’t worry, ah fan is so thoughtful, must have drawn a picture:

 

Let’s also look at these six states separately:

  • NEW to RUNNABLE: Thread calls start

    • There are two main ways to create a Thread object: inherit the Thread object and override the run() method, or implement the Runnable interface and override the run() method. The implementation class is used as a parameter to create the Thread object
    • But remember, NEW is just saying, this thread is created at the programming language level, but it’s not created at the operating system level, so of course it won’t be scheduled by the operating system, let alone executed
    • So if a Java thread wants to execute, it must transition to the RUNNABLE state, where thread calls the start method
  • RUNNABLE and BLOCKED. If a thread waits for a synchronized implicit lock, it moves from RUNNABLE to BLOCKED. Because synchronized modified methods/blocks allow only one thread to execute at a time, the other threads must wait, and when the waiting thread obtains a synchronized implicit lock, it goes from BLOCKED to RUNNABLE

    • Is there a question here? A thread waiting for a condition occurs at the OPERATING system level. At the JVM level, Java thread state does not change. The state of the Java thread is still RUNNABLE
  • Transition between RUNNABLE and WAITING state, I feel the diagram has been well stated, I will not repeat it here

  • TIMED_WAITING state transition between RUNNABLE and TIMED_WAITING state transition, I think the diagram has been well stated, and I will not repeat it here. After careful observation, it will be found that TIMED_WAITING has more timeout parameters than WAITING state transition. After all, TIMED_WAITING is a time-bound waiting

  • The thread is TERMINATED automatically after the run() method is executed. It is TERMINATED if an exception is thrown during the run() process

    • Sometimes we might want to forcibly break execution of the run() method. What do we do? Should I use stop() or interrupt()? The correct posture is to call the interrupt() method
    • The stop() method actually kills the thread, giving it no respite. If the thread that is killed has a synchronized implicit lock, it can never be released, and subsequent threads can’t acquire a synchronized lock. Isn’t that particularly dangerous? Methods suspend() and resume() are also not recommended
    • Interrupt () is much gentler than stop(). It simply tells the thread that it doesn’t need to do something later. The thread can choose to do it now and not do it. Just to let you know. For example, if you want to take a train to a place and suddenly notice that the train is late, you can choose to ignore the notice and continue to wait, or choose another high speed train, but no matter what you do, it has nothing to do with the railway station, its responsibility to notify has been exhausted

It should make sense when you look at this

In the Java thread lifecycle, the RUNNABLE state is a combination of ready and RUNNING states, while BLOCKED, WAITING, and TIMED_WAITING are actually WAITING states. That is, a thread must wait for some event to happen before it can proceed

So much for the Life cycle of Java threads

Draw a picture, explain, and talk to the interviewer for half an hour, right?

Finally, learning Java not only rely on their own efforts, but also to stand on the shoulders of giants, learning some open source code at the same time should see more technical documents and extremely technical video;

It is precisely because of the selfless dedication and sharing spirit of the predecessors and the continuous open source technology that we, the laters, can easily obtain more excellent learning resources and make the Internet industry develop rapidly. Here, I also want to do a selfless dedication of the small programmer, I want to do a dissemination of resources

Big factory interview real question

Java Architecture Books

The resume template

Need friends like + collection after the private letter “learn” to get the way