- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在安排具有特定延迟的任务来处理我的项目:
while (currentPosition < count) {
ExtractItemsProcessor extractItemsProcessor =
getExtractItemsProcessor(currentPosition, currentPositionLogger);
executor.schedule(extractItemsProcessor, waitBetweenProzesses, TimeUnit.SECONDS);
waitBetweenProzesses += sleepTime;
currentPosition += chunkSize;
}
我如何安排例如 3 个任务(具有 3 个线程的执行器),并且每个线程在完成其任务后必须等待 10 秒?
最佳答案
您可以使用 Executors.newFixedThreadPool(NB_THREADS) 来返回 ExecutorService。然后使用这个 executorService 您可以提交任务。示例:
private static final int NB_THREADS = 3;
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(NB_THREADS);
for (int i = 0; i < NB_THREADS; i++) {
final int nb = i + 1;
Runnable task = new Runnable() {
public void run() {
System.out.println("Task " + nb);
try {
TimeUnit.SECONDS.sleep(10);
System.out.println("Task " + nb + " terminated");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Error during thread await " + e); // Logging framework should be here
}
}
};
executorService.submit(task);
}
executorService.shutdown();
try {
executorService.awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Error during thread await " + e);
}
}
它将并行运行 3 个任务,输出如下所示:
Task 1
Task 3
Task 2
Task1 terminated
Task2 terminated
Task3 terminated
在你的情况下,你可以这样做:
ExecutorService executorService = Executors.newFixedThreadPool(NB_THREADS);
while (currentPosition < count) {
ExtractItemsProcessor extractItemsProcessor =
getExtractItemsProcessor(currentPosition, currentPositionLogger);
executorService.submit(extractItemsProcessor); // In processor you should add the sleep method
waitBetweenProzesses += sleepTime;
currentPosition += chunkSize;
}
关于java - 如何根据线程数量调度并发Runnables,而线程完成任务后必须等待延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55418327/
好吧,我的问题听起来很困惑,但实际上很简单。我有一个 Runnable 启动另一个 Runnable 实例。所以基本上: 可运行 1 -> 可运行 2 runnable1 是在 runnable2 还
我正在尝试使用我的单例的 Handler.post() 方法从另一个 runnable 运行一个 runnable,但是直到原始 runnable 完成后才运行第二个 runnable。在下面的示例代
为什么下面的第一个例子不起作用? run(R::new); 方法 R.run 未被调用。 run(new R()); 方法 R.run 被调用。 这两个示例都是可编译的。 public class C
为什么下面的代码不起作用?基本上,这是一个更困难的程序的简化版本,在该程序中,我试图创建一个可运行的初始屏幕,其中包含一些选择,然后有链接到不同可运行项的按钮,但这并没有按照我的预期运行。 impor
我有一个带有主选项卡 Activity 的 Android 应用,以及各个选项卡中的多个 Activity 。在我的主要 Activity 的 onCreate() 中,我有一个创建列表的可运行文件,
我正在使用 Mockito 进行测试。我有一个回调接口(interface): interface Callback { void onMessageRetrieved(String mess
这个问题在这里已经有了答案: Does postDelayed cause the message to jump to the front of the queue? (1 个回答) 关闭 7 年
我想将 runnable 发布到 runnable 内的 View 对象,目前我被困在这里。 var runnable = Runnable { if(numLinesToDraw
假设我有一个 ExecutorService,我用它来运行 Job 对象,这是一个扩展 Runnable 的类。我希望我的 Job 对象并行运行,除了让 2 个 Job 对象具有相同的 job.get
下面是该类的源代码。 我想验证如何 shutdownNow()适用于未提交的任务。我在下面的代码中遇到的问题是 shutdownNow()返回 List而不是 List我已经提交了 List包含提交的
我正在尝试找出如何从多线程应用程序中获得最大性能。 我有一个这样创建的线程池: ExecutorService executor = Executors.newFixedThreadPool(8);
这有什么区别?请引用选项1和选项2。因为我遇到了麻烦,因为它们好像是一样的。它们运行正确 Thread ThreadPoolExecutor executor = (ThreadPoolExecuto
当任何命令在任何 ScheduledExecutorService 上以固定速率调度时,它返回 ScheduledFuture 也可以取消。但是“cancel”并不能保证命令在 cancel 返回后仍
因此,我希望在尝试保持轻资源负载的同时同时完成几件事。例如,同时播放声音和更新 GUI。让多个处理程序具有单个可运行对象或单个处理程序具有多个并行运行的可运行对象更好吗? 我知道下面的实现实际上不会同
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Java: “implements Runnable” vs. “extends Thread” 我只是想知道创建自
为什么 Java 的 scheduleWithFixedDelay 使用 Runnable 而不是包装 runnable 的 FutureTask? 这可以很容易地用两个不同的代码示例来展示: Sch
这个问题在这里已经有了答案: What's the difference between Activity.runOnUiThread(runnable action) and Handler.pos
我有如下三个类: 1.线程实例 public class ThreadInstance extends Thread { public ThreadInstance() { }
春天给了我下面的错误,却想不出为什么!。注意:在XML文件中没有对此的Bean定义。。我也提出了这一点,但没有为我的案件找到任何解决方案
一 Runnable 的职责 Runnable 接口非常简单,只定义了一个无参无返回值的 run 方法,源码如下。 @FunctionalInterface public interface Runn
我是一名优秀的程序员,十分优秀!