views:

76

answers:

3

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

Usage of Semaphore permits is understood by me.

Thanks for any response.

+3  A: 

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:

Going to 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.

IE: 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 CoutndownLatch 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.

John V.
@John W. : I saw LockSupport is used in `AbstractQueuedLongSynchronizer`, do you suggest it is a sort of internal API class? Just wanted to see any illustrative piece of code for it...
Max
@Max LockSupport is a utility class that anyone can use. Its not necessarily an internal class Unsafe.java is a good example of an internal class. LockSupport is public so that others can write their own version of AQS
John V.
@John W. : Thanks John, I know it's for extension. Your answer made me rephrase a question: generally the use of `AbstractQueuedSynchronizer` appears vague for me.
Max
+1  A: 

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

irreputable
+1  A: 

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.

Jed Wesley-Smith