任何LockSupport& AbstractQueuedSynchronizer使用? [英] Any practical example of LockSupport & AbstractQueuedSynchronizer use?

查看:99
本文介绍了任何LockSupport& AbstractQueuedSynchronizer使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计,任何人都可以给出一个简单的实例: LockSupport & AbstractQueuedSynchronizer use?示例在 javadocs 相当紧张。



semaphore 的用法被我理解。



感谢任何回应。

解决方案

如果你谈论使用锁定机制(甚至同步屏障)只需使用 java.util.concurrent.Lock 。显而易见的建议是使用 ReentrantLock ,代表 Synch 。同步是一个AQS,它依次使用 LockSupport



p>

编辑:



不允许我们详细讨论 AbstractQueuedSynchronizer (AQS)。



并发构造虽然在用法上可以有很大的不同,它们可以有相同的底层函数。



p在某些情况下停下这个线程。在一些其他条件下唤醒一个线程。



这是一个非常广泛的指令集,但很明显,大多数并发结构需要一些通用的功能,能够为它们处理这些操作。输入AQS。有五个主要的同步障碍。




  • ReentrantLock

  • ReadLock

  • WriteLock

  • Semaphore

  • CountDownLatch



现在,所有这五种结构在使用它们时都有非常不同的规则集。 A CountdownLatch 可以允许许多线程同时运行,但强制一个(或多个)线程等待,直到至少n个线程减少所述锁存器。 p>

ReentrantLock 每次只强制一个线程进入临界区,并将所有其他线程排队等待完成。



ReadLock 允许任何数量的读取线程进入临界区,直到获取写入锁。 p>

示例可以继续,但是这里的大图片是他们都使用AQS。这是因为它们能够使用AQS提供的原语函数,并在其上实现更复杂的功能。 AQS允许你停下来打开和唤醒线程(如果需要的话可以中断),但是你可以支持许多复杂的函数。


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.

解决方案

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.

这篇关于任何LockSupport& AbstractQueuedSynchronizer使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆