Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lock.java and LockSupport.java: what purpose we have LockSupport?

I have used basic object.wait() , object.notify() , object.notifyAll() in multi-threaded programming.

I know we have the package java.util.concurrent and it has java.util.concurrent.locks package. Specifically in java.util.concurrent.locks we have : Condition, Lock and LockSupport (among others).

I read online about this package, and understood basics of Lock and Condition. However, I didn't understand LockSupport. I did search to understand LockSupport , however didn't find anything relevant which can help me understand what it is used for. I have seen that in LockSupport we have methods like park() , unPark() etc. However, I didn't get what is the purpose of LockSupport, it seems to be doing more or less same like Lock?

Can anyone please help me understand why we have LockSupport and what it does that Lock don't do.

like image 998
CuriousMind Avatar asked Sep 06 '25 02:09

CuriousMind


1 Answers

It's a helper class with very low level concurrency mechanisms that are used by the other classes. Unless you want to write your own higher level concurrency structures, you probably won't use it.

There are other helper classes like java.util.concurrent.locks.AbstractQueuedSynchronizer, which provide other mechanisms (e.g. wait queues in case of AQS) to the classes that you are more likely to use, like ReentrantLock and so on.

like image 61
Kayaman Avatar answered Sep 07 '25 20:09

Kayaman