1 回答

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個(gè)贊
它真的沒有魔法。您所要做的就是多次提交相同的任務(wù),如下所示:
public static void main(String args[]) {
int numberOfThreads = 2;
ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads);
Runnable task1 = () -> {
System.out.println("Executing Task1 inside : " +
Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException ex) {
throw new IllegalStateException(ex);
}
};
executorService.submit(task1);
executorService.submit(task1);
}
添加回答
舉報(bào)