如何启动 1K 线程并在线程完成后连续运行同一任务
如果我创建 1K 线程并使用锁存器同时启动它们,一旦线程完成,我的进程就会结束。
我想要做的是,当线程结束时,启动另一个线程来处理同一任务(或者以某种方式让同一线程再次继续处理同一任务)。
场景:
我想要启动 1K 线程,并且不希望在处理完成后启动另外 1K 线程而导致性能损失。
线程只是与 url http://www.example.com/some/page< 建立 http url 连接/a>
我想要做的是连续运行 x 秒,并且始终有 1K 线程运行。
我不想为此使用执行器来学习如何在没有执行器的情况下执行此操作,并且我相信由于执行器框架将任务和线程分开,因此它不能保证同时运行多少个线程。
If I create 1K threads and launch them at the same time using a latch, once the threads complete my process ends.
What I want to do is, as the thread ends, start up another thread to work on the same task (or somehow get the same thread to continue processing with the same task again).
Scenario:
I want to start 1K threads, and don't want the performance penalty of starting another 1K threads when they finish processing.
The threads simply make a http url connection to a url http://www.example.com/some/page
What I want to do is continuously run for x seconds, and always have 1K threads running.
I don't want to use an executor for this for both learning how to do it w/o it and I believe since the executor framework separates the task and threads, it doesn't gaurantee how many threads are running at the same time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须在 Runnable 本身中执行此操作。创建一个围绕您的操作的简单循环。
如果您希望它们在某个点同步,请创建一个计数为 1000 的 CountdownLatch,并在每次迭代结束时执行倒计时并等待。
Apache JMeter 是一个免费的性能测试工具,您可以轻松配置它以在多个线程中测试 URL。它还可以将测试分发到例如 10 个客户端执行 100 个线程。
You'll have to do it in the Runnable itself. Create a simple loop surrounding your actions.
If you want them all to synchronize at a certain point, create a CountdownLatch with count 1000 and at the end of every iteration do a countDown and await.
Apache JMeter is a free performance testing tool that you can easily configure to test URL's in multiple threads. It can also distribute the tests to have e.g. 10 clients doing 100 threads instead.
在
run()
方法中使用循环。use a loop in your
run()
method.据我所知,您希望拥有大量服务器线程,并让它们执行列表中的一项工作,然后返回并等待指定另一项工作(或处理另一项已经-列表中当前的作品)。
这就是您使用队列的目的。 BlockingQueue 可能是最简单的形式,可以满足您的目的,并且 JDK 中有多种实现。
Close as I can tell, you want to have a large number of server threads, and have them execute a piece of work from a list, then come back and wait for another piece of work to be be specified (or work on another already-present piece in the list).
This is what you use a queue for. Probably a BlockingQueue is the simplest form to use that will suit your purposes, and there are several implementations of this in the JDK.