事件驱动的未来- 线程池
我们使用 callable
和 Future
从线程池接收终止线程的结果。我们应该调用get()
来接收返回的结果。我的问题是:它不是事件驱动的。是否有任何框架可以为 C 中的子进程获取类似 SIGCHLD
的结果? 我想要这样的东西:(当池中的每个线程完成工作时,线程池将调用这个函数)
public void fetchResult(Thread t, Runnable r, Future<Integer> result) {
Integer x = result.get();
/* handle x */
/* also we have Thread and Runnable object that has terminated */
}
We use callable<V>
and Future<V>
to receive the result of a terminated thread from a thread pool. We should call get()
to receive the returned result. My problem is: it is not event driven. Is there any framework to get result like SIGCHLD
for child processes in C?
I want something like this:(the thread pool will call this function when each thread in the pool finished the job)
public void fetchResult(Thread t, Runnable r, Future<Integer> result) {
Integer x = result.get();
/* handle x */
/* also we have Thread and Runnable object that has terminated */
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想查看 ThreadPoolExecutor.afterExecute() 方法。每个任务完成后都会调用它。您可以创建 ThreadPoolExecutor 的自定义子类,它具有您想要的基于事件的回调行为。
you might want to check out the ThreadPoolExecutor.afterExecute() method. it's called after each task completes. you could create a custom subclass of ThreadPoolExecutor which has the event based callback behavior you desire.
您可以轻松创建事件驱动模板。以下伪代码说明了一种方法。
类可以扩展模板
,可以实例化
You could easily create an event driven template. The following pseudo-code illustrates one approach.
Classes can the extend the template
Which can be instantiated