首页 \ 问答 \ Java Executor:小任务还是大任务?(Java Executor: Small tasks or big ones?)

Java Executor:小任务还是大任务?(Java Executor: Small tasks or big ones?)

考虑一个可以分解成数百个小的,可独立运行的任务的大任务。 更具体地说,每个小任务是发送轻型网络请求并决定从服务器接收的答案。 这些小任务预计不会花费超过一秒的时间,并且总共需要几台服务器。

我想到了使用Executor框架实现这一点的两种方法,我想知道哪一个更好,为什么。

  1. 创建一些,比如5到10个任务,每个任务涉及做一堆发送和接收。
  2. 为每个发送和接收创建单个任务(可调用或可运行),并安排执行者运行所有这些任务(数百个)。

我很抱歉,如果我的问题表明我懒得测试这些,并亲自看看哪些更好(至少在性能方面)。 在回答这个具体案例时,我的问题有一个更为笼统的方面。 在这种情况下,当你想使用执行程序来完成所有的调度和其他工作时,创建大量小任务或将它们分组到更少数量的更大任务中会更好吗?


Consider one big task which could be broken into hundreds of small, independently-runnable tasks. To be more specific, each small task is to send a light network request and decide upon the answer received from the server. These small tasks are not expected to take longer than a second, and involve a few servers in total.

I have in mind two approaches to implement this using the Executor framework, and I want to know which one's better and why.

  1. Create a few, say 5 to 10 tasks each involving doing a bunch of send and receives.
  2. Create a single task (Callable or Runnable) for each send & receive and schedule all of them (hundreds) to be run by the executor.

I'm sorry if my question shows that I'm lazy to test these and see for myself what's better (at least performance-wise). My question, while looking after an answer to this specific case, has a more general aspect. In situations like these when you want to use an executor to do all the scheduling and other stuff, is it better to create lots of small tasks or to group those into a less number of bigger tasks?


原文:https://stackoverflow.com/questions/12717251
更新时间:2023-05-23 22:05

最满意答案

同步调度会暂停代码的执行,直到调度块完成为止。 异步调度立即返回,该块与调用代码异步执行:

dispatch_sync(somewhere, ^{ something });
// Reached later, when the block is finished.

dispatch_async(somewhere, ^{ something });
// Reached immediately. The block might be waiting
// to be executed, executing or already finished.

并且有两种调度队列,串行和并发。 串行的按照添加顺序严格逐个发送块。 当一个完成时,另一个开始。 这种执行只需要一个线程。 并发队列并行地同时调度块。 那里使用了更多的线程。

如果您认为合适,您可以混合使用同步/异步调度和串行/并发队列。 如果您想要使用GCD来保护对关键部分的访问,请使用单个串行队列并分配此队列上共享数据的所有操作(同步或异步,无关紧要)。 这样,共享数据总是只有一个块操作:

- (void) addFoo: (id) foo {
    dispatch_sync(guardingQueue, ^{ [sharedFooArray addObject:foo]; });
}

- (void) removeFoo: (id) foo {
    dispatch_sync(guardingQueue, ^{ [sharedFooArray removeObject:foo]; });
}

现在,如果guardingQueue是一个串行队列,即使从不同线程同时调用addFoo:removeFoo:方法,add / remove操作也不会发生冲突。


Synchronous dispatch suspends the execution of your code until the dispatched block has finished. Asynchronous dispatch returns immediately, the block is executed asynchronously with regard to the calling code:

dispatch_sync(somewhere, ^{ something });
// Reached later, when the block is finished.

dispatch_async(somewhere, ^{ something });
// Reached immediately. The block might be waiting
// to be executed, executing or already finished.

And there are two kinds of dispatch queues, serial and concurrent. The serial ones dispatch the blocks strictly one by one in the order they are being added. When one finishes, another one starts. There is only one thread needed for this kind of execution. The concurrent queues dispatch the blocks concurrently, in parallel. There are more threads being used there.

You can mix and match sync/async dispatch and serial/concurrent queues as you see fit. If you want to use GCD to guard access to a critical section, use a single serial queue and dispatch all operations on the shared data on this queue (synchronously or asynchronously, does not matter). That way there will always be just one block operating with the shared data:

- (void) addFoo: (id) foo {
    dispatch_sync(guardingQueue, ^{ [sharedFooArray addObject:foo]; });
}

- (void) removeFoo: (id) foo {
    dispatch_sync(guardingQueue, ^{ [sharedFooArray removeObject:foo]; });
}

Now if guardingQueue is a serial queue, the add/remove operations can never clash even if the addFoo: and removeFoo: methods are called concurrently from different threads.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。