Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AbstractQueuedSynchronizer in Java concurrent

What is AbstractQueuedSynchronizer in Java's concurrent.locks package used for? Can someone shed some light on its methods doAcquireInterruptibly and parkAndCheckInterrupt?

like image 390
Hemanshu Avatar asked Mar 10 '12 07:03

Hemanshu


1 Answers

  • AbstractQueuedSynchronizer: It provides a framework for implementing blocking locks and related synchronizers like semaphores, CountDownLatch etc. The basic algorithm for acquire is try acquire, if successful return else enqueue thread, if it is not already queued and block the current thread. Similarly basic algorithm for release is try release, if successful, unblock the first thread in the queue else simply return. The threads will wait in a first-in-first-out (FIFO) wait queues. The abstract methods tryAcquire and tryRelease will be implemented by subclasses based on their need.

  • doAcquireInterruptibly will try to acquire the lock. If the lock is already acquired by some other thread, the current thread will be blocked(parked). If it acquires the lock, it will simply return.

  • parkAndCheckInterrupt will park the thread or in other words disable the thread scheduling till some other thread unblocks it. It can be due to the release of lock by the owning thread or due to some other thread interrupting it. If it is interrupted by some other thread an exception will be thrown.
like image 158
Satish Avatar answered Oct 12 '22 23:10

Satish