简体   繁体   English

获取具有优先级的信号量阻塞线程的方法?

[英]Acquire method of Semaphore blocked thread with priority?

Semaphore sema = new Semaphore(1);信号量 sema = 新信号量(1);

Create a Semaphore object and the default initialization, the only one license, when multiple threads at the same time trying to get the license, must change is only one thread can access permission, then other threads will wait outside, when the first thread to release license, then wait for the thread has the right to obtain the license or is it just the first to arrive and wait for the thread shall be entitled to Who can help me, I will appreciate it very much创建一个Semaphore对象并默认初始化,只有一个license,当多个线程同时尝试获取license时,必须改变的是只有一个线程可以访问权限,然后其他线程会在外面等待,当第一个线程释放时license,然后等待线程有权获得许可证还是只是第一个到达等待线程应有权谁可以帮助我,我将非常感谢

From the Semaphore javadoc :Semaphore javadoc

The constructor for this class optionally accepts a fairness parameter.此类的构造函数可以选择接受公平参数。 When set false, this class makes no guarantees about the order in which threads acquire permits.当设置为 false 时,此类不保证线程获取许可的顺序。 In particular, barging is permitted, that is, a thread invoking acquire() can be allocated a permit ahead of a thread that has been waiting - logically the new thread places itself at the head of the queue of waiting threads.特别是, barging是允许的,也就是说,可以在一个一直在等待的线程之前为调用acquire()的线程分配一个permit——逻辑上,新线程将自己置于等待线程队列的头部。 When fairness is set true, the semaphore guarantees that threads invoking any of the acquire methods are selected to obtain permits in the order in which their invocation of those methods was processed (first-in-first-out; FIFO).当公平性设置为真时,信号量保证调用任何acquire方法的线程被选择以按照它们对这些方法的调用的处理顺序(先进先出;FIFO)获得许可。 Note that FIFO ordering necessarily applies to specific internal points of execution within these methods.请注意,FIFO 排序必然适用于这些方法中的特定内部执行点。 So, it is possible for one thread to invoke acquire before another, but reach the ordering point after the other, and similarly upon return from the method.因此,一个线程可能在另一个线程之前调用acquire ,但在另一个线程之后到达排序点,从方法返回时也是如此。 Also note that the untimed tryAcquire methods do not honor the fairness setting, but will take any permits that are available.另请注意,未计时的tryAcquire方法不遵守公平设置,但会采用任何可用的许可。

So, you may choose when initializing a Semaphore between two different orderings:因此,您可以选择在两种不同的顺序之间初始化Semaphore

  • If you initialize with new Semaphore(1, true) , then when multiple threads are waiting, the first thread that called acquire() will be the first to receive a permit.如果您使用new Semaphore(1, true)初始化,那么当多个线程正在等待时,调用acquire()的第一个线程将首先获得许可。 That is, permits will be given to threads in the order that the threads requested them.也就是说,将按照线程请求它们的顺序向线程授予许可。
  • If instead you initialize with new Semaphore(1, false) or equivalently new Semaphore(1) , then whenever a thread calls acquire() , it will become the first thread in line to get a permit.相反,如果您使用new Semaphore(1, false)等效的new Semaphore(1)初始化,那么每当线程调用acquire() ,它将成为队列中第一个获得许可的线程。 That is, when a permit becomes available, the last thread to call acquire() will be the first thread to receive it.也就是说,当许可可用时,最后一个调用acquire()的线程将是第一个接收它的线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM