执行大量任务的最佳实践 [英] Best practice for executing large number of tasks

查看:73
本文介绍了执行大量任务的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在循环中创建了大量可运行的对象,即使只有5个踏步也可以在那里处理任务。有什么办法可以使内存中仅存在5个可运行对象,而不是任务总数(100000)。

Below code creates large number of runnable object in loop, even only 5 treads are there to process the tasks. Is there any way so that at any moment only 5 runnable object will be present in memory not the total number of tasks( 100000).

ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10000; i++) {
    Runnable worker = new WorkerThread("" + i);
    System.out.println("ExecutorExample.main()");
    executor.execute(worker);
}


推荐答案

随时存在的要求内存中将仅存在5个可运行对象,限制太多。任务完成后,将不再有下一个任务要运行,并且将花费一些时间来创建另一个任务。最好有5个runnung任务和5个排队等待。为了限制队列中的任务数量,请对可运行对象使用固定大小的BlockingQueue,例如

The requirement that at any moment only 5 runnable object will be present in memory is too restrictive. When a task finishes, there would be no next task to run, and some time would be spend to create another task. Better to have 5 runnung tasks and 5 waiting in a line. In order to restrict the number of the tasks in the queue, use a BlockingQueue of fixed size for runnables, e.g.

ThreadPoolExecutor executor = new ThreadPoolExecutor(
     5, 5,
     10L, TimeUnit.MILLISECONDS,
     new LinkedBlockingQueue<Runnable>(5)
)

UPDT要避免RejectedExecutionException,请按照 https:的建议添加RejectedExecutionHandler。 //stackoverflow.com/a/3518588/1206301

UPDT To avoid RejectedExecutionException, add RejectedExecutionHandler, as proposed in https://stackoverflow.com/a/3518588/1206301:

RejectedExecutionHandler block = new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            try {
                executor.getQueue().put(r);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    executor.setRejectedExecutionHandler(block);

这篇关于执行大量任务的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆