- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在我的项目中,到目前为止,我使用 CyclicBarrier
来“同步”多个线程(每个线程都运行相同类型的 Runnable
) .在我的例子中,由于同步频率高,使用 CyclicBarrier
被证明是低效的,但忙等待机制可能工作得更快。这是我到目前为止得到的(一些部分被遗漏了):
public class MyRunnable implements Runnable {
private static AtomicInteger counter = null; // initialized to the number
// of threads
public void run() {
// do work up to a "common point"
synchronized (this) {
// decrement the counter and - if necessary - reset it
if (counter.decrementAndGet() == 0) {
counter.set(numberOfThreads);
// make all the busy waiting threads exit from the loop
for (int i = 0; i < threads.length; i++)
threads[i].interrupt();
}
}
// busy wait until all threads have reached the "common point"
while (!Thread.interrupted()) {}
}
}
不幸的是,这段代码的性能比 CyclicBarrier
还要差。 Here's一个简短的、可编译的例子。关于如何改进它有什么建议吗?
最佳答案
如果你有更多的处理器然后你有线程运行,这里的忙等待只会“更快”地工作。如果你不断地在 Thread.interrupted 上旋转并且只是在消耗 CPU 时间,你实际上会显着降低性能。
CyclicBarrier/CountDownLatch 出了什么问题?这似乎是一个更好的解决方案。
关于Java——实现忙等待机制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6553044/
根据https://developers.google.com/google-apps/calendar/v3/reference/freebusy/query处的文档要执行空闲/忙碌查询,您必须在正
我在启动 Apache 后使用 XAMPP 1.7.7 我收到此警报: Busy... Apache started [Port 80] 我已经打开了 httpd.conf 文件,并将端口号更改为 8
我有端口问题。 每当我启动 Apache 时,它都会给我这个错误: Busy - Apache Started [port 80] 当我启动 Mysql 时出现以下错误: Busy - ERROR
当我运行我的代码时,我总是得到后台工作人员很忙。有帮助吗? struct FtpSetting { public string Server { get; set
在我们的 Android 和 iOS MVVMCross 应用程序中,我们偶尔会遇到 SQLiteException: busy 异常。 给定下面的代码,我们有几个存储库,每个存储库都构造一个下面的实
每次运行解决方案(20 projs)时,我都会收到此错误。我尝试禁用 UI 选项、resharper、删除未使用的扩展等,但仍然无法通过此错误。 我使用的是 VS 2015 Update 2 Ente
我正在使用这个插件 https://github.com/mozilla/rust-android-gradle ,这需要我添加 tasks.whenTaskAdded { task -> i
我是一名优秀的程序员,十分优秀!