我想实现一个软件,它可以确保 10 人threads一直在运行同一个课程
所有人threads都会在随机的几秒钟后终止,但是,我想确保一旦它们被终止,它们就会立即被 new created 替换threads。
你知道是否有可用的图书馆吗?
非常感谢,弗拉德
我想实现一个软件,它可以确保 10 人threads一直在运行同一个课程
所有人threads都会在随机的几秒钟后终止,但是,我想确保一旦它们被终止,它们就会立即被 new created 替换threads。
你知道是否有可用的图书馆吗?
非常感谢,弗拉德
而是使用线程池。JavaExecutor框架提供了它。
ExecutorService executor = Executors.newFixedThreadPool(10);
ExecutorService executorPool = Executors.newFixedThreadPool(noOfThreads);
公共静态 ExecutorService newFixedThreadPool(int nThreads)
Creates a thread pool that reuses a fixed set of threads operating off a
shared unbounded queue. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.
Parameters:
nThreads - the number of threads in the pool
Returns:
the newly created thread pool