如何每秒执行成千上万的异步操作,每个操作有三秒钟的等待时间?

时间:2016-10-08 13:48:16

标签: java node.js concurrency

我需要在一个应用程序中每秒执行数万个IO绑定操作,每个应用程序都有3秒的延迟。在node.js中,这可以通过以下方式轻松完成:

var events = require('events');
var eventEmitter = new events.EventEmitter();

var count = 0;

var connectHandler = function connected() {
    setTimeout(function() {
        count++;
        console.log('task complete ' + count);
    }, 3000);
}

for (var i = 0; i < 30000; i++) {
    eventEmitter.on('mockedIOTask', connectHandler);
}

eventEmitter.emit('mockedIOTask');

如何在Java应用程序中实现?我尝试了以下但它一次只执行3个任务(原因很明显):

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;

public class Main {
    public static void main(String[] args) {
        //ExecutorService threadPool = Executors.newCachedThreadPool();
        for (int i = 0; i < 30000; i++) {
            final int count = i;
            ForkJoinPool.commonPool().submit(() -> {
                try {
                    Thread.sleep(3000);
                    System.out.println("task complete" + count);
                } catch (InterruptedException e) {
                }
            });
        }
        // Keep the program alive
        try {
            Thread.sleep(50000);
        } catch (InterruptedException e) {
        }
    }
} 

1 个答案:

答案 0 :(得分:1)

您可以使用ScheduledExecutorService

在3秒后执行30K任务
ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
List<Future<?>> tasks = new ArrayList<>();
for (int i = 0; i < 30000; i++) {
    final int count = i;
    tasks.add(ses.submit(() -> System.out.println("task complete" + count)));
}
for (Future f : tasks) f.get();
ses.shutdown();

如果你想添加IO绑定操作,我会先启动阻塞操作,然后再迁移到使用像netty这样的库来处理非阻塞IO。