多线程工作者模式
我不确定该模式的确切名称(代码示例),但我确信它存在。 我有一个类作为客户端并连接到服务器。 我想以多线程运行它。但这是诀窍。 我想跟踪每个线程完成整个进程操作所需的时间。 如果操作时间在指定范围内,则应启动更多线程,依此类推 直到线程太多并且速度变慢。
我说清楚了吗?
请指教...
I'm not sure about the exact name of that pattern (code sample), but I'm sure it exists.
I have a class which works as a client and connects to the server.
I would like to run it in multithreaded. But here is the trick.
I want to keep track of the time it takes to complete whole process operation per thread.
And if operation time is in the specified bound, then more threads should be started and so on
until there too much threads and speed goes lower.
Am I clear?
Please advise...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法通过添加更多线程来更快地完成单个请求。
另外,如果您的所有请求都发送到同一台服务器,那么您实际上可能会通过发出多个并发请求来减慢服务器的速度。撇开其他考虑因素(例如达到最大并发连接数)不谈。
因此,它比
“在多线程中运行”
更复杂!You can't make an individual request complete faster by adding more threads.
Also, if all your requests are to the same server, you may actually slow down the server by making multiple concurrent requests. Quite apart from other considerations like hitting the maximum number of concurrent connections.
So, it's more complex than just
"run it in multithreaded"
!模式的关键在于它不仅仅是一种。
听起来您想要一个
TaskListener
来侦听taskStarted
和taskFinished
事件,这可能会使用TaskProfiler
来确定该任务
是否在适当的时间内完成。 (如果您的阈值对于所有任务来说并不统一,它可能还需要访问
任务来确定有关它的更多详细信息)从那里听起来您想要一个< code>TaskExecutor 它将在启动和完成特定任务时通知
TaskListener
。TaskExecutor
可能会提供可用的increaseWorkerPool
和decreaseWorkerPool
方法,这些方法可以由从探查器接收反馈的TaskAdministrator
进行调整/听众。这主要是说明性的,您可能只想将任务完成时间作为单个
taskComplete(Task task, long millis)
方法的一部分传递。综上所述,“生成线程直到它不能很好地工作”并不是负载平衡技术中最具启发性的......
The key to patterns is it's not about just one.
It sounds like you want a
TaskListener
that listens fortaskStarted
andtaskFinished
events, which may use aTaskProfiler
which will determine if thatTask
completed in an appropriate amount of time. (It may also want toVisit
theTask
to ascertain more details about it, if your thresholds are not uniform for all tasks)From there it sounds like you then want a
TaskExecutor
which will notify theTaskListener
when it starts and finishes a particular task.The
TaskExecutor
might make availableincreaseWorkerPool
anddecreaseWorkerPool
methods which can be adjusted by aTaskAdministrator
which receives feedback from the profiler/listener.This is mostly demonstrative, you might just want to pass the task completion time as part of a single,
taskComplete(Task task, long millis)
method.All of that having been said, "spawn threads until it doesn't work very good" isn't the most heuristic of load balancing techniques...