- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在 Java java.util.concurrent.Semaphore 文档中,我不太清楚如果 semaphore.acquire() 阻塞线程并随后被 InterruptedException 中断会发生什么。信号量值是否已经减小,是否需要释放信号量?
目前我正在使用这样的代码:
try {
// use semaphore to limit number of parallel threads
semaphore.acquire();
doMyWork();
}
finally {
semaphore.release();
}
或者在 acquire() 期间发生 InterruptedException 时我是否应该不调用 release() ?
最佳答案
call release() when an InterruptedException occurs during acquire() ?
你不应该。如果 .acquire() 被中断,则不会获取信号量,因此可能不应该释放它。
你的代码应该是
// use semaphore to limit number of parallel threads
semaphore.acquire();
try {
doMyWork();
}
finally {
semaphore.release();
}
关于java - 如果 semaphore.acquire() 得到 InterruptedException,需要 semaphore.relase()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12104978/
condition.acquire(threading.Condition()) 是否类似于lock.acquire(threading.Lock)。两者都可以访问锁吗?我可以使用condition.
我正在尝试执行一个非常基本的示例,演示 accumulate() 的使用Drools 的函数,但出现 java.lang.NullPointerException 异常。 代码如下: Metric.j
我是java初学者,我正在尝试信号量。我尝试编写一个具有编写器和读取器的代码,我只尝试使用 acquire() 和 release(): 1) If a writer is writing, then
我的一个程序中有一个奇怪的问题,一个线程获取一个条件,而另一个线程告诉我没有获取该条件。 为了知道线程是否获得了条件,我做了一些调试信息,看起来是的,他做到了。但是另一个线程告诉我条件没有获得。 这是
我的类(class)有一个互斥锁,定义如下: ACE_Mutex m_specsMutex; 当我使用不带参数的 acquire() 方法时,一切正常。但是当我将它与时间值一起使用时(如下所示),它会
我使用 QSharedMemory 和 QSystemSemaphore 来组织多个进程之间的事件交换。 QSharedMemory 存储接收者的数量和事件数据。接收者在 QSystemSemapho
我正在为我的 REST 服务编写一个 API 库。在某些时候,访问 token 将需要更新。我正在尝试实现一种线程安全的方法来执行此操作,以便仅发送一个更新请求,即使多个线程可能想要同时更新它。 这是
我正在我的node.js 项目中使用MySQL 数据库。我用 Knex 创建了一个数据库查询,结果没问题。但是当我再次尝试查询时,出现此错误: Error: Unable to acquire a c
以下锁定机制用于防止 cron 作业并发运行: #!/bin/bash echo "Before critical section" ( flock -e 200 echo "In c
在 Python 3.4.3 上,我无法理解 threading.Lock.acquire() 如何阻塞,直到锁定状态设置为解锁。 threading.Lock 似乎是上面链接的 _dummy_thr
我有一段代码 locked = lock.acquire(False) if locked: break 根据 python 文档: lock.aquire(False):-
-Thread 1- y.store (20, memory_order_release); x.store (10, memory_order_release); -T
我最近在运行测试时遇到了这个错误。我在本地 MongoDB 服务器 (4.0.5) 上试过了,我也在 Mongo Atlas 上试过了,但遇到了同样的问题。 我尝试增加锁定超时,但没有效果。 我不确定
在Windows平台上,TCriticalSection是通过调用Windows API EnterCriticalSection/LeaveCriticalSection来实现的。 Microsof
我为我的数据库使用 mssql ( https://www.npmjs.com/package/mssql ) 模块。通常我使用导致 pg ( https://www.npmjs.com/packag
我在 Python 中有这个示例,它演示了条件变量的使用。 import logging import threading import time logging.basicConfig(level=
我希望我的应用保持 CPU 运行但关闭屏幕以最大限度地减少电力浪费。 关于此主题的先前帖子建议采用以下方法: mPm = (PowerManager) getSystemService(Con
是否可以在 C#.NET 应用程序中请求 Windows 7 PC 上的管理权限? 我希望能够通过 Click Once 部署应用程序,并让用户使用它来执行管理任务(在本例中,它正在为主应用程序编写注
我正在更新 Delphi (Delphi 2009) 代码,它专门使用 TCriticalSection.Acquire/Release 对,而不是 Enter/Release 或 Leave 对。我
我试图理解使用条件变量时丢失唤醒的问题。我相信我已经使用了下面正确的设计模式。消费者: lock the mutex while the condition is not satisfied
我是一名优秀的程序员,十分优秀!