繁体   English   中英

Java信号量是否使用忙等待或默认等待/通知?

[英]Do Java semaphores use busy waiting or a wait/notify by default?

问题是这样的。 我在Windows 7上使用JDK 6.0,并尝试使用信号量作为解决同步问题的机制。 它工作得很好,但我试图避免忙于等待我的问题。

我只想问一下java文档并省去了麻烦,但文档是这样的:

Acquires the given number of permits from this semaphore,
 blocking until all are available, or the thread is interrupted.

Acquires the given number of permits, if they are available,
 and returns immediately, reducing the number of available permits
 by the given amount.

If insufficient permits are available then the current thread
 becomes disabled for thread scheduling purposes and lies dormant

http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Semaphore.html#acquire(int

也就是说,文档似乎暗示了两个答案。 哪一个是正确的?

我看不出它是怎么意味着忙着等待的。 它清楚地表明该线程被“禁用”并处于休眠状态。 基本上,它很便宜:线程在等待获取信号量时不会消耗处理器时间。

由于这条线,它显然是等待/通知:

如果没有足够的许可证,那么当前线程将被禁用以进行线程调度并处于休眠状态。

这意味着操作系统不会调度线程,直到唤醒它的事件(可用信号量允许)发生,此时线程被发出信号以继续执行。

暂无
暂无

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

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