This is the 22nd day of my participation in Gwen Challenge.

Then the previous content “understand the life cycle of threads (a)”, a comprehensive answer to the question raised at the beginning of the article.

Third, answer questions

3.1TIME-WAITING WAITING What’s the difference?

WAITING is dead, unless an exception is generated or released. Time-waiting is a timed WAITING. In addition to the trigger condition of WAITING, it also adopts the strategy that I am not stupid, I only wait for the TIME I want to wait.

3.2BLOCKED*WAITINGWhat are the state differences?

Java’s official definition of BLOCKED is thread. State:

A thread that is blocked waiting for A monitor lock is in this state.

The thread is in this state while waiting for the Monitor to lock.

The definition of WAITING is:

A thread that is waiting indefinitely for another thread to perform a particular action is in this state.

A thread that waits indefinitely for another thread to perform a particular operation is in this state.

Ha ha, is it also very confused, I also. Because the authorities just explained what it was, but didn’t compare the two.

BLOCKED is itself BLOCKED, so it’s ok to think of it as a special WAITING. WAITING means that the current thread is WAITING for some work, so the current thread is idle. BLOCKED represents that the current thread is busy completing work, but another thread is in the way, so the current thread is now idle.

In case you still don’t understand, here’s a chart.

BLOCKED WAITING
When other threads notifyWAITINGThread of state,WAITINGThreads in state are reversed to this state; The thread will remain in this state for a long time until the object lock is acquired. Threads use # in synchronized code blockswait,#joinMethod keeps this thread in this state unless another thread calls the same object#notify,#notifyAllMethods.
Blocking is waiting for another thread to release the lock. Blocking is waiting for notification from another thread.
Do not interrupt Can be interrupted by

If that doesn’t make sense to you, I’ll just zoom in, as shown below.

In the figure, there are two threads competing for the lock resource. Thread 1 first obtains the lock resource and is in the RUNNING state, and then executes the #wait method and is in the WAITING state. Thread 2 obtains the lock resource through CPU scheduling and is in the RUNNING state. Thread 2 calls the #notify method on the same object, and thread 1 receives notification that it is BLOCKED and waiting for the object lock Monitor to be released. When thread 2 finishes executing, the lock is released. Thread 1 acquires the lock and its status changes to RUNNING.

3.3sleep,wait,join,yieldWhat are the effects of each function execution on the thread?

Before we talk about their impact on threads, it’s important to recognize that threads are competing not only for lock resources, but also for CPU resources.

sleep wait join yield
Put the thread in theWAITINGstate Put the thread in theWAITINGstate Put the thread in theWAITINGstate The execution completion thread is still inRUNNINGState (ready state)
The release ofCPUResource, does not release the lock resource The release ofCPU, lock resources The release ofCPU, lock resources The release ofCPUResource, does not release the lock resource
When the timeout is complete, reverse toRUNNINGState (ready state) After being notified, reverse toBLOCKEDstate After being notified, reverse toBLOCKEDstate It could happen right awayRUNNINGThe ready state becomesRUNNINGState (running state)
Thread method Object methods Object methods Thread method
It does not have to be executed within a synchronized code block Must be executed within a synchronized code block It does not have to be executed within a synchronized code block It does not have to be executed within a synchronized code block
Blocking threads, to some extent, serializes the execution process to achieve synchronization Blocking threads, used for communication between threads Leave the current thread waiting for execution#joinMethod to serialize the execution process to achieve synchronization Pause the thread to give programs of the same priority a chance to execute

Note: The main thread calls the #sleep method of another thread, which will only sleep the current thread.


Brother boy, don’t panic to go! Leave a thumbs-up and comment on the discussion. Welcome to the column face interview don’t panic | Java concurrent programming, a raise don’t have to worry about the interview. Also welcome to pay attention to me, must be a longer better man.