- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是 Java 编程的新手,但遇到了一个问题。我正在使用 Spring TaskExecutor
接口(interface)进行线程池管理。我必须并行地从不同来源(Http、文件、数据库)提取内容,因此我为此使用了 TaskExecutor
。现在我希望一旦所有线程都完成执行,它应该指示 TaskExecutor
,如果它们没有在 4 秒内完成执行,则任务应该被终止。所以我被这个问题困住了。我尝试将可调用接口(interface)与 future 一起使用,但这会导致任务同步执行,但我需要异步。请帮帮我。
最佳答案
您还可以在创建任务后创建一个循环并检查超时:
ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Config.xml");
ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor) context.getBean("taskExecutor");
taskExecutor.execute(new PrintTask("YOUR TASK ONE"));
taskExecutor.execute(new PrintTask("YOUR TASK TWO"));
double timeOutMs = 3000.0 ; // 3 seconds of maximum duration
double startTime = System.currentTimeMillis() ;
//check active thread, if zero then shut down the thread pool
for (;;) {
int count = taskExecutor.getActiveCount();
System.out.println("Active Threads : " + count);
if (System.currentTimeMillis() - startTime > timeOutMs) {
// Do something : its to late, cancel the tasks !
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (count == 0) {
taskExecutor.shutdown();
break;
}
}
}
关于java - Spring 任务执行器 :How to get notifed when all task complete execution and also if they dont in some time slot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6529883/
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
这个问题困扰了我一段时间。有大量关于响应式图像的教程和问答,但我还没有找到一个像样的,它可以解释我如何保持肖像图像的尺寸不拉伸(stretch)到它们所在的硬币容器的宽度。 我有什么: 我使用 Wor
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
很难说出这里要问什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。为了帮助澄清这个问题以便可以重新打开它,visit the help center .
我是 xquery 的菜鸟,我正在“测试”代码 我不可能在混凝土中过滤一个 xml。 我不确定,但第一个标签似乎有问题,它包含一个属性,并且可能我解析错误...... XML 示例
嘿伙计们,我有一个代码,当您单击按钮时,它会从一种状态更改为另一种状态(它会启动视频并混合)。现在我尝试说我的事件处理程序如果有人点击速度非常快,他不应该将其用作输入。像 = Eventhandler
请帮助我(我不明白问题是什么。它给出了一个错误和文本“ERROR”,我在日志中找不到任何内容。我是初学者,目前正在学习 android 开发。 RequestQueue requestQueu
我正在开发一个基于 iFrame 的 facebook 选项卡应用程序。除了处理用户单击“不允许”的情况外,我在授权方面一切正常。但是我被困在这里了。在昨晚花了几个小时试图解决这些问题后,我不知道从这
我知道 @SuppressWarnings("unchecked") 批注可用于在泛型未按预期准确使用时抑制编译器警告。我明白,从纯粹的角度来看,人们几乎不应该使用这些注释,而应该重构代码,这样就没有
我正在尝试使用 css3 在我的网站中创建一个对话框,如果用户使用 adblock,该对话框就会弹出。该对话框基本上要求用户在我的网站上启用广告,但我不想每次他/她访问我的网站时弹出对话框来打扰用户,
我创建了一个卷 docker volume create postgresql_db 现在我在我的 docker-compose.xml 中使用它 services: postgresql:
选择器 ID 和函数:为什么不起作用? HTML: JS: $("input").mouseover(function(){ alert('111'); }); $("input#textU
#compile time flags CFLAGS=-Wall -Werror CC=gcc all: prog-1-gcc prog-2-gcc prog-3-gcc prog-4-gcc
我是 headfirst 设计模式的读者,我注意到了这一点。 “好莱坞原则,别叫我们,我们叫你” 这意味着高级组件告诉低级组件“不要调用我们,我们调用你” High-Level Component 是
我有这个简单的 plpgsql 函数: CREATE FUNCTION "update_times" () RETURNS trigger AS ' BEGIN NEW.upd
我制作了一个表单,如果 id“Telefoninumber”存在,该表单将更新表“Komentaar”。但即使我输入不存在的 ID,我仍然得到回显“记录已成功更新”。 最佳答案 如果执行查询,您正在
我想寻求帮助来理解这段代码...我是 mysql 的初学者,所以我发现很难阅读这段代码的作用... SELECT b.bcode FROM (SELECT bcode FROM tbl_items
您知道为什么即使没有 Size Class 更改也会调用 traitCollectionDidChange 吗? 选择一个 UILabel,我在 Storyboard属性检查器/已安装 UILabel
当您看到网站时 http://colin.amsterdam你会在顶部看到社交链接,当你点击它时(Facebook 和 Instagram 链接)它又变成了蓝色。在 CSS 中,我为 a:visite
我在模型中实现了一些 ImageField 并安装了 PIL(不是最干净的安装)。当我在管理中获得上传按钮时,事情似乎正常,当我在 View 中调用 .url 属性时,我得到带有文件名及其上传属性的字
我是一名优秀的程序员,十分优秀!