Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Will LockSupport.park () release lock resources?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

LockSupport.park() will release lock resources, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.

introduction

As you know, I recently in recruitment, met a classmate today, his source code read some, and then I began to ask the AQS chain.

Me: Tell me about the general process of AQS?

He: AQS contains a state variable, a synchronization queue... balabala... mutex balabala, shared balabala...

Me: What queues are there in AQS besides synchronization queues?

He: There is also a Condition, and there is a conditional queue in the Condition...

Me: What is the difference between conditional queues and synchronous queues?

He: conditional queue balabala, then call LockSupport.park() to go to sleep, wait to be awakened,…, balabala

What is the difference between LockSupport.park() and Thread.sleep()?

Thread.sleep() does not release lock resources,…, balabala

Me: Does LockSupport.park() release lock resources?

He: Yes. Confused with Object.wait()

Me: Will it? Will it? Will it?

He (shyly lowered his head): Brother Tong, I don't know, you didn't write in the article. (This is what I wrote.)

OK, today we will see if LockSupport.park() will release lock resources.

Difference between Thread.sleep() and Object.wait()

First of all, let's look at the difference between Thread.sleep() and Object.wait(). This is a bad street topic. Everyone should be able to say two points.

Thread.sleep() does not release the lock it holds, Object.wait() does;

(2) Thread.sleep() must be passed time, Object.wait() can be passed or not, not passed means always blocking;

Thread.sleep() will wake up automatically when the time is up, and then continue to execute;

(4) Object.wait() without time, need another thread to wake up with Object.notify();

(5) Object.wait() with time, if not notified, to the time will automatically wake up, then divided into two cases, one is immediately acquired lock, thread will naturally continue to execute; second, there is no immediate acquisition lock, thread into the synchronization queue waiting to acquire lock;

In fact, the biggest difference between the two is that Thread.sleep() does not release lock resources, Object.wait() will release lock resources.

Difference between Thread.sleep() and Condition.await()

Let's look at the difference between Thread.sleep() and Condition.await().

In fact, this topic is similar to the above topic, because the principle of Object.wait() and Condition.await() is similar, you can refer to the article "Life Cycle of Java Thread Series" written by Brother Tong.

The answer to this question is basically the same as Object.wait(), except that Condition.await() calls LockSupport.park() to block the current thread.

In fact, it also does two things before blocking the current thread, one is to add the current thread to the conditional queue, the other is to "completely" release the lock, that is, let the state variable become 0, and then call LockSupport.park() to block the current thread, you can refer to the article "ReentrantLock source code analysis of Java synchronization series (II)--conditional lock" written by Brother Tong.

Seeing here, is there an answer to the question raised at the beginning of today?[This article was originally written by the public from the number "Tong Ge reads the source code"]

Difference between Thread.sleep() and LockSupport.park()

LockSupport.park() has several sibling methods-parkNanos(), parkUtil(), etc. The park() method we are talking about here is collectively referred to as this class of methods.

(1) Functionally, Thread.sleep() and LockSupport.park() methods are similar, both blocking the execution of the current thread, and neither will release the lock resources occupied by the current thread;

(2) Thread.sleep() cannot wake up from the outside, can only wake up by itself;

(3) LockSupport.park() method can be awakened by another thread calling LockSupport.unpark() method;

(4) Thread.sleep() method declaration throws an InterruptedException, so the caller needs to catch this exception or throw it again;

(5) LockSupport.park() method does not need to catch interrupt exceptions;

Thread.sleep() itself is a native method;

(7) LockSupport.park() is the underlying Unsafe native method called;

Difference between Object.wait() and LockSupport.park()

Both block the current thread, so what's the difference? After the above analysis, I believe you must be very clear, really? Look down!

(1) The Object.wait() method needs to be executed in a synchronized block;

LockSupport.park() can be executed anywhere;

(3) Object.wait() method declaration throws an interrupt exception, the caller needs to catch or throw again;

(4) LockSupport.park() does not need to catch interrupt exceptions [this article was originally written by the public from the number "Tongge reading source code"];

(5) Object.wait() without timeout, another thread needs to execute notify() to wake up, but does not necessarily continue to execute subsequent content;

(6) LockSupport.park() without timeout, need another thread to execute unpark() to wake up, will certainly continue to execute subsequent content;

(7) What happens if notify() is executed before wait()? IllegalMonitorStateException thrown;

(8) What happens if unpark() is executed before park()? Thread will not be blocked, skip park() directly, continue to execute subsequent content;

Did you miss the last two points?!

In fact, in the article "Java thread series to write a thread pool by yourself (continued)" in the code comments slightly mentioned unpark() this method, it executes first, then the subsequent park() method will no longer work.

The underlying principle of park()/unpark() is a "binary semaphore", which you can think of as a Semaphore with only one license, except that this semaphore does not increase the number of licenses when unpark() is repeated, and only has one license at most.

For the content of semaphore, you can refer to the article "Semaphore source code analysis of Java synchronization series".

Does LockSupport.park() release lock resources?

No, it is only responsible for blocking the current thread, releasing lock resources is actually implemented in the await() method of Condition.

eggs

OK, so we cross-referenced Thread.sleep(), Object.wait(), Condition.await(), LockSupport.park().

Let's end today with a Mind Map.

Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report