简体   繁体   English

Java fork-join执行程序对数据库访问的用法

[英]java fork-join executor usage for db access

The ForkJoinTask explicitly calls out "Subdividable tasks should also not perform blocking I/O ". ForkJoinTask显式调出“可细分的任务也不应执行阻塞的I / O ”。 It's primary aim is " computational tasks calculating pure functions or operating on purely isolated objects". 它的主要目标是“ 计算任务以计算纯函数或对纯隔离对象进行操作”。 My question is :- 我的问题是:

  1. Why design the ForkJoinTask to restrict blocking IO tasks? 为什么要设计ForkJoinTask来限制阻塞的IO任务?
  2. What are the gotchas if i do implement a blocking IO task? 如果我确实执行阻塞IO任务,该怎么办?
  3. How come both spring and play frameworks, are full of examples using fork-join executors for DB calls? spring和play框架为何都包含使用fork-join执行程序进行数据库调用的示例?

In my scenario, a single request does two types of works, one of which is encryption, which pushes CPU core to 100% for 200 ms and second, few database calls. 在我的场景中,单个请求执行两种类型的工作,其中一种是加密,它将200毫秒内的CPU内核推到100%,第二次是很少的数据库调用。 Any kind of static partitioning such as 6 threads for encryption and 2 threads for blocking IO, will not provide optimal usage of the CPU. 诸如6个用于加密的线程和2个用于阻止IO的线程之类的静态分区都无法提供CPU的最佳使用。 Hence, having a fork-join executor, with certain level of over provisioning in number of threads over total CPU count, coupled with work stealing, would ensure better usage of CPU resources. 因此,拥有一个fork-join执行器,并且在一定数量的总CPU数量上过度配置了一定数量的线程,再加上工作偷窃,将确保更好地利用CPU资源。

Is my above assumption and understanding around forkjoin executor correct and if not, please point me towards the gap. 我对forkjoin执行器的上述假设和理解是否正确,如果不正确,请指出空白。

Why design the ForkJoinTask to restrict blocking IO tasks? 为什么要设计ForkJoinTask来限制阻塞的IO任务?

underlying the fork join pool is shared amount of threads, if there's some IO work blocking on those threads, then less threads for CPU intensive work. 分叉联接池的底层是共享的线程数量,如果这些线程上有一些IO工作受阻,则用于CPU密集型工作的线程会更少。 other none blocking work will starve. 其他没有人阻止工作。

What are the gotchas if i do implement a blocking IO task? 如果我确实执行阻塞IO任务,该怎么办?

typically, FJPool allocated thread about the number of processors. 通常,FJPool为线程分配有关处理器数量的信息。 so if you do have to use IO blocking on threads, make sure you allocate enough threads for your other tasks. 因此,如果必须在线程上使用IO阻塞,请确保为其他任务分配足够的线程。

you can also iso late your IO work on dedicated threads that is not shared with FJ pool. 您还可以在与FJ池不共享的专用线程上隔离IO工作。 but you call blocking IO, your thread blocks and get scheduled for other task until unblocked 但您调用blocking IO,则线程被阻止并安排执行其他任务,直到取消阻止

How come both spring and play frameworks, are full of examples using fork-join executors for DB calls? spring和play框架为何都包含使用fork-join执行程序进行数据库调用的示例?

play is no different. play也没什么不同。 they use dedicated pools for IO task, so other task won't suffer. 他们将专用pools用于IO任务,因此其他任务不会受到影响。

The Framework does not restrict any type of processing. 框架不限制任何类型的处理。 It is not recommended to do blocking, etc. I wrote a critique about this framework years ago, here is the point on the recommendations. 不建议进行阻塞等。几年前,我对这个框架进行了评论,这是建议的重点。 This was for the Java7 version but it is still applicable for Java8. 该版本适用于Java7版本,但仍适用于Java8。

Blocking is not fatal, sprint and play block and they work just fine. 阻塞不是致命的,冲刺和游戏阻塞,它们工作得很好。 You need to be careful when using Java8 since there is a default common-fork/join pool and tying up threads there may have consequences for other users. 使用Java8时需要小心,因为有一个默认的公共叉/连接池,并且绑定线程可能会对其他用户造成影响。 You could always define your own f/j pool with the additional overhead, but at least you wouldn't interfere with others using the common pool. 您总是可以使用额外的开销定义自己的f / j池,但是至少您不会使用公共池来干扰其他人。

Your scenario doesn't look bad. 您的情况看起来还不错。 You're not waiting for replies from the internet. 您不必等待来自互联网的回复。 Give it a try. 试试看。 If you run into difficulty with stalling threads, look into the ForkJoinPool.ManagedBlocker interface. 如果遇到使线程停止的困难,请查看ForkJoinPool.ManagedBlocker接口。 Using that interface informs the f/j pool that you are doing blocking calls and the framework will create compensation threads. 使用该接口会通知f / j池您正在执行阻塞调用,框架将创建补偿线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM