我有一个函数 foo()
我想在间隔上被多个线程调用。示例:想要每100ms执行使用x线数量的foo()。我不确定该怎么做。
我正在寻找您可以在其中实例化具有数量工人的线程池。但是,当您使用此线程池调用时,它会一次耗尽所有线程并执行任务吗?
任何想法都将不胜感激;谢谢
I have a function foo()
that I want to be invoked by multiple threads on an interval. example: want to execute foo() every 100ms with x number of threads. I am unsure how to go about it.
I am looking at FixedThreadPool where you can instantiate a thread pool with number of workers. However when you invoke using this thread pool does it exhaust all threads at once and executes a task?
any ideas would be appreciated; thanks
发布评论
评论(1)
对于重复单个任务,您不需要多个线程。
使用单线线程计划的执行人服务。
将任务定义为
可运行
或callable
。安排重复执行。
所有这些已经在堆栈溢出上已经覆盖了很多次。搜索以了解更多。
For a single task being repeated, you do not need multiple threads.
Use a single-threaded scheduled executor service.
Define your task as a
Runnable
orCallable
.Schedule for repeated execution.
All of this has been covered many times already on Stack Overflow. Search to learn more.