简体   繁体   English

java multithreading - 等待空闲线程可用性来创建和分配下一个任务

[英]java multithreading -Wait for free threads availability to create and assign next task

Looking for an approach to solve a multi threading problem.寻找解决多线程问题的方法。 I have N number of tasks say 100. I need to run this 100 tasks using limited number of threads say 4. Task size is huge , so I dont want to create all the tasks together.我有 N 个任务,比如 100。我需要使用有限数量的线程来运行这 100 个任务,比如 4。任务大小很大,所以我不想一起创建所有任务。 Each task will be created only when a free thread is available from the pool.只有当池中有空闲线程可用时,才会创建每个任务。 Any recommended solution for the same.任何推荐的解决方案。

You could use a BlockingQueue to define the tasks.您可以使用BlockingQueue来定义任务。 Have one thread create the tasks and add them to the queue using put , which blocks until there's space in the queue.让一个线程创建任务并使用put将它们添加到队列中,它会阻塞直到队列中有空间。 Then have each worker thread just pull the next task off of the queue.然后让每个工作线程从队列中取出下一个任务。 The queue's blocking nature will basically force that first thread (that's defining the tasks) to not get too far ahead of the workers.队列的阻塞性质基本上会强制第一个线程(即定义任务)不要领先于工作线程太远。

This is really just a case of theproducer-consumer pattern , where the thing being produced and consumed is a request to do some work.这实际上只是生产者-消费者模式的一个例子,其中被生产和消费的东西是做一些工作的请求。

You'll need to specify some way for the whole thing to finish once all of the work is done.一旦所有工作完成,您需要指定某种方式来完成整个事情。 One way to do this is to put N "poison pills" on the queue when the generating thread has created all of the tasks.一种方法是在生成线程创建所有任务时将 N 个“毒丸”放在队列中。 These are special tasks that just tell the worker thread to exit (rather than doing some work and then asking for the next item).这些特殊任务只是告诉工作线程退出(而不是做一些工作然后要求下一项)。 Since each thread can only read at most one poison pill (because it exits after it reads it), and you put N poison pills in the queue, you'll ensure that each of your N threads will see exactly one poison pill.由于每个线程最多只能读取一个毒丸(因为它读取后退出),并且您将 N 个毒丸放入队列中,因此您将确保您的 N 个线程中的每个线程都只能看到一个毒丸。

Note that if the task-generating thread consumes resources, like a database connection to read tasks from, those resources will be held until all of the tasks have been generated -- which could be a while!请注意,如果任务生成线程消耗资源,例如读取任务的数据库连接,则这些资源将被保留,直到所有任务都已生成——这可能需要一段时间! That's not generally a good idea, so this approach isn't a good one in those cases.这通常不是一个好主意,因此在这些情况下这种方法不是一个好方法。

If can get the number of active threads at a certain point of time from the thread pool you can solve your problem.如果可以从线程池中获取某个时间点的活动线程数,则可以解决您的问题。 To do that you can use ThreadPoolExecutor#getActiveCount .为此,您可以使用ThreadPoolExecutor#getActiveCount Once you have the number of the active thread then you can decide you should create a task or not.获得活动线程的数量后,您就可以决定是否应该创建任务。

ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);
executor.getActiveCount();

Note: ExecutorService does not provide getActiveCount method, you have to use ThreadPoolExecutor .注意:ExecutorService 不提供getActiveCount方法,你必须使用ThreadPoolExecutor ThreadPoolExecutor#getActiveCount Returns the approximate number of threads that are actively executing tasks. ThreadPoolExecutor#getActiveCount返回正在积极执行任务的线程的approximate数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM