简体   繁体   English

线程受限的多个执行路径

[英]Multiple Execution Paths with limited Threads

I have a challenging asynchronous programming task, wonder if anyone did anything like that with Java. 我有一个艰巨的异步编程任务,想知道是否有人用Java做过类似的事情。 Let's assume I'm developing a framework which will be used like this: 假设我正在开发一个框架,它将像这样使用:

  1. Do some custom Java stuff 做一些自定义Java的东西
  2. Call Framework Method 调用框架方法
  3. Do some custom Java stuff 做一些自定义Java的东西
  4. Call Framework Method 调用框架方法

The above code is Synchronous from the user perspective. 从用户角度来看,以上代码是同步的。 However the Framework Methods internally perform some long asynchronous operation and blocking the User thread for the duration of the operation is not efficient/feasible. 但是,框架方法在内部执行一些长时间的异步操作,并且在该操作期间阻止用户线程是无效/可行的。 Blocking the User thread is not feasible since I might need to support thousands of concurrent executions of steps 1-4... In other words I'm looking for a way to support X concurrent Java code executions using Y threads where X > Y... It's somewhat similar to what a Reactor pattern is doing with IO, but more generic.. Any thoughts? 阻塞用户线程是不可行的,因为我可能需要支持数千个步骤1-4的并发执行。换句话说,我正在寻找一种方法来支持使用Y线程(其中X> Y)执行X个并发Java代码执行。 ..有点类似于Reactor模式对IO进行的操作,但是更通用。.有什么想法吗?

the normal thread pools/executors will help you with that 普通的线程池/执行程序将帮助您

if you can split up the framework jobs in several atomic tasks that will activate other tasks; 如果您可以将框架作业拆分为几个原子任务,这些任务将激活其他任务; the jobs will be able to execute with less active threads than there are jobs to run with decent throughput 这些作业将能够以较少的活动线程执行,而执行的作业数量却要比正常运行的作业少

对于初学者,请使用java.util.concurrent形式的类,例如Executors

Starting with JDK 1.5 you have the Concurrency API at hand. 从JDK 1.5开始,您可以使用Concurrency API。 The High Level Concurrency Objects within The Java Tutorial give you a good start at it. Java教程中的高级并发对象为您提供了一个良好的开端。

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

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