- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我的问题与 this one here 密切相关.正如在那里发布的那样,我希望主线程等到工作队列为空并且所有任务都已完成。然而,我的情况的问题是,每个任务都可能递归地导致提交新任务进行处理。这使得收集所有这些任务的 future 有点尴尬。
我们当前的解决方案使用忙等待循环来等待终止:
do { //Wait until we are done the processing
try {
Thread.sleep(200);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} while (!executor.getQueue().isEmpty()
|| numTasks.longValue() > executor.getCompletedTaskCount());
numTasks 是一个随着每个新任务的创建而增加的值。这行得通,但由于等待繁忙,我认为这不是很好。我在想有没有什么好的方法可以让主线程同步等待,直到被显式唤醒。
最佳答案
非常感谢您的所有建议!
最后我选择了我认为相当简单的东西。我发现 CountDownLatch几乎是我需要的。它会阻塞,直到计数器达到 0。唯一的问题是它只能倒计时,不能倒计时,因此在我的动态设置中不起作用,任务可以提交新任务。因此,我实现了一个新类 CountLatch
,它提供了额外的功能。 (见下文)我然后按如下方式使用这个类。
主线程调用 latch.awaitZero()
,阻塞直到 latch 达到 0。
任何线程,在调用 executor.execute(..)
之前调用 latch.increment()
。
任何任务,在即将完成之前,都会调用 latch.decrement()
。
当最后一个任务终止时,计数器将达到 0,从而释放主线程。
欢迎提出进一步的建议和反馈!
public class CountLatch {
@SuppressWarnings("serial")
private static final class Sync extends AbstractQueuedSynchronizer {
Sync(int count) {
setState(count);
}
int getCount() {
return getState();
}
protected int tryAcquireShared(int acquires) {
return (getState() == 0) ? 1 : -1;
}
protected int acquireNonBlocking(int acquires) {
// increment count
for (;;) {
int c = getState();
int nextc = c + 1;
if (compareAndSetState(c, nextc))
return 1;
}
}
protected boolean tryReleaseShared(int releases) {
// Decrement count; signal when transition to zero
for (;;) {
int c = getState();
if (c == 0)
return false;
int nextc = c - 1;
if (compareAndSetState(c, nextc))
return nextc == 0;
}
}
}
private final Sync sync;
public CountLatch(int count) {
this.sync = new Sync(count);
}
public void awaitZero() throws InterruptedException {
sync.acquireSharedInterruptibly(1);
}
public boolean awaitZero(long timeout, TimeUnit unit) throws InterruptedException {
return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));
}
public void increment() {
sync.acquireNonBlocking(1);
}
public void decrement() {
sync.releaseShared(1);
}
public String toString() {
return super.toString() + "[Count = " + sync.getCount() + "]";
}
}
请注意,increment()
/decrement()
调用可以按照建议封装到自定义的 Executor
子类中,例如,由 Sami Korhonen 编写,或者按照 impl 的建议使用 beforeExecute
和 afterExecute
。看这里:
public class CountingThreadPoolExecutor extends ThreadPoolExecutor {
protected final CountLatch numRunningTasks = new CountLatch(0);
public CountingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}
@Override
public void execute(Runnable command) {
numRunningTasks.increment();
super.execute(command);
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
numRunningTasks.decrement();
super.afterExecute(r, t);
}
/**
* Awaits the completion of all spawned tasks.
*/
public void awaitCompletion() throws InterruptedException {
numRunningTasks.awaitZero();
}
/**
* Awaits the completion of all spawned tasks.
*/
public void awaitCompletion(long timeout, TimeUnit unit) throws InterruptedException {
numRunningTasks.awaitZero(timeout, unit);
}
}
关于java - 执行者 : How to synchronously wait until all tasks have finished if tasks are created recursively?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14535770/
这两者有区别吗? startActivity(intent); finish(); startActivity(intent); ThisActivityName.this.finish(); 注意:
我有一个包含大量 url 链接的 arrayOfLinks。我需要从这些链接中获取图像。我正在使用以下代码来执行此操作。 - (void)getImages { NSArray *links
我想创建一条执行以下步骤的路线 读取文件 将文件分割成行 使用 BeanIO 解码行 调用(昂贵且耗时的)处理器 我想在处理完所有线路后完成这条路线。 所以我的路线的基本布局如下所示: from("d
我正在使用 Nodejs。我有一个异步的 forEach,因为我必须在 forEach 中等待结果。结果,我需要等待 forEach 完成,然后继续处理循环的结果。我找到了几种等待 forEach 的
我正在编写需要背靠背内核调用的 android renderscript 代码(有时一个内核的输出成为另一个内核的输入)。我还有一些全局指针,绑定(bind)到 Java 层的内存。每个内核更新那些全
伪代码: void draw() { Vertex* vertices = scene.GetVertexArray(); glEnableClientState(...);
当我创建一个 Android 应用程序时,我首先在其中创建了主 Activity ,然后添加了启动 Activity 以及一个正常 Activity 。 所以我的问题是每当我在应用程序中单击退出时,它
我正在尝试将一些 API 数据获取到我的应用程序,并且我有两个 Activity 。第一个是启动屏幕(就像加载应用程序时谷歌使用的那样),我想知道在哪里调用 finish() 来结束 Activity
我使用 创建了“新文件” Activity startActivityForResult(new Intent(MainActivity.this, NewFile.class),1); NewFil
我有一个以 SingleTask 模式 (Android 2.2) 运行的 Activity ,该 Activity 从其他 Activity 或服务接收 Intent 。我的 Activity 将每
我编写了以下代码来使用 TextView 和按钮显示消息框。单击按钮时, Activity 应该完成。但这有时会起作用,但并不一致。有时它会挂起。如何让它始终如一地工作?请帮忙 public clas
我正在搜索,当 onDestroy 在 android 应用程序上执行时,我发现它是在设备资源不足(RAM、CPU) 时执行的,并且当用户调用 finish() 时。 例如,当我按下后退按钮从一个 A
我正在编写一个 Android 应用程序,它显示一个带有一些元素和一些 CRUD 操作的 RecyclerView。我想做的一件事是从我的 RecyclerView 更新现有元素,为此我打开一个新 A
被测试的 Activity 是一个简单的棋盘游戏。当满足游戏结束的必要条件时,在执行几项操作以显示谁赢了等之后,将运行以下代码: // Return to main menu after 5 seco
我有一个 Activity ,它创建了我创建的名为classA的类的新实例。在classA中,我有一个方法将对象添加到位于classA中的STATICarrayList。 所以我的 Activity
我正在使用 Facebook Android SDK,并希望在用户登录并获取用户对象后关闭我的 Activity。实际上,我存储了它的一部分,但无论如何我都想关闭该 Activity 。
我以前从事c# dotnet开发,有一个Application.exit()类型的方法,可以立即关闭应用程序并释放内存。 我发现 android 的“finish()”方法应该这样做 但它没有这样做,
我有一个 Activity ,我调用了 finish() 方法,但 Activity 没有从内存中清除。 调用 finish() 后,我看到方法 onDestroy() 已成功执行(并且我清除了其中的
我想在我的MKMapView上设置一个区域,然后找到与 map 的东北角和西南角对应的坐标。 This code works just fine to do that: //Recenter and
我正在尝试使用从顶部滑动动画将一个 View 替换为另一个 View 。我有点遵循 http://gentlebytes.com/2011/09/auto-layout-in-lion/ 中发布的指南
我是一名优秀的程序员,十分优秀!