多线程工作者模式

发布于 2024-12-19 05:59:52 字数 165 浏览 0 评论 0原文

我不确定该模式的确切名称(代码示例),但我确信它存在。 我有一个类作为客户端并连接到服务器。 我想以多线程运行它。但这是诀窍。 我想跟踪每个线程完成整个进程操作所需的时间。 如果操作时间在指定范围内,则应启动更多线程,依此类推 直到线程太多并且速度变慢。

我说清楚了吗?

请指教...

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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千仐 2024-12-26 05:59:52

您无法通过添加更多线程来更快地完成单个请求。

另外,如果您的所有请求都发送到同一台服务器,那么您实际上可能会通过发出多个并发请求来减慢服务器的速度。撇开其他考虑因素(例如达到最大并发连接数)不谈。

因此,它比“在多线程中运行”更复杂!

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"!

风铃鹿 2024-12-26 05:59:52

模式的关键在于它不仅仅是一种。

听起来您想要一个 TaskListener 来侦听 taskStartedtaskFinished 事件,这可能会使用 TaskProfiler 来确定该任务是否在适当的时间内完成。 (如果您的阈值对于所有任务来说并不统一,它可能还需要访问任务来确定有关它的更多详细信息)

从那里听起来您想要一个< code>TaskExecutor 它将在启动和完成特定任务时通知 TaskListener

TaskExecutor 可能会提供可用的 increaseWorkerPooldecreaseWorkerPool 方法,这些方法可以由从探查器接收反馈的 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 for taskStarted and taskFinished events, which may use a TaskProfiler which will determine if that Task completed in an appropriate amount of time. (It may also want to Visit the Task 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 the TaskListener when it starts and finishes a particular task.

The TaskExecutor might make available increaseWorkerPool and decreaseWorkerPool methods which can be adjusted by a TaskAdministrator 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文