Any practical example of LockSupport & AbstractQueuedSynchronizer use?

前端 未结 5 1226
情深已故
情深已故 2020-12-28 20:24

Guys, can anyone give a simple practical example of LockSupport & AbstractQueuedSynchronizer use? Example given in javadocs is quite strained.<

相关标签:
5条回答
  • 2020-12-28 20:40

    AQS is a wonderful class for building concurrency primitives – but it is complex and requires a bit of study to use it properly. I have used it for a few things like lazy initialisation and a simple fast reusable latch.

    As complex as it is, I don't think AQS is particularly vague, it has excellent javadocs describing how to use it properly.

    0 讨论(0)
  • 2020-12-28 20:47

    If youre talking about using a locking mechanism (or even sync barriers) just use a java.util.concurrent.Lock. The obvious suggestion is to user a ReentrantLock which delegates to a Synch. The synch is an AQS which in turn uses LockSupport.

    Its all done under the covers for you.

    Edit:

    No let's go over the practical uses of AbstractQueuedSynchronizer (AQS).

    Concurrency constructs though can be very different in their usage all can have the same underlying functions.

    I.e. Under some condition park this thread. Under some other condition wake a thread up.

    This is a very broad set of instructions but makes it obvious that most concurrency structures would need some common functionality that would be able to handle those operations for them. Enter AQS. There are five major synchronization barriers.

    • ReentrantLock
    • ReadLock
    • WriteLock
    • Semaphore
    • CountDownLatch

    Now, all these five structures have very different set of rules when using them. A CountdownLatch can allow many threads to run at the same time but forces one (or more) threads to wait until at least n number of threads count down on said latch.

    ReentrantLock forces only one thread at a time to enter a critical section and queues up all other threads to wait for it to completed.

    ReadLock allows any number of reading threads into the critical section until a write lock is acquiered.

    The examples can go on, but the big picture here is they all use AQS. This is because they are able to use the primitive functions that AQS offers and implements more complex functionality on top of it. AQS allows you to park unpark and wake up threads ( interruptibly if need be) but in such a way that you can support many complex functions.

    0 讨论(0)
  • 2020-12-28 20:50

    they are not meant for direct use in client code; more for helping building new concurrent classes.

    0 讨论(0)
  • 2020-12-28 20:50

    2.7 release of Disruptor uses LockSupport.parkNanos instead of Thread.sleep to reduce latency:

    http://code.google.com/p/disruptor/

    0 讨论(0)
  • 2020-12-28 20:50

    AFAIK, AbstractQueuedSynchronizer is used to manage state transitions. The JDK uses it to extend Sync, an internal class for java.util.concurrent.FutureTask. The Sync class manages the states (READY, RUNNING, RAN, and CANCELLED) of FutureTask and the transitions between them.

    This allows, as you may know, FutureTask to block on FutureTask.get() until the RAN state is reached, for example.

    0 讨论(0)
提交回复
热议问题