Limiting the number of threads and Java concurrency

I have not been able to find an example of this specific case using the latest JAVA parallel procedures.

I am planning to use it threads

to process items from an open queue that can contain from 0 to thousands of requests. I want to constrain so that at any given time there are no less than 0 and no more than 10 threads processing queue items.

Is there a Java parallel process for this particular case?

+3


source to share


4 answers


I think the thread pool is what you are looking for. Take a look at ExecutorService and Executors.

ExecutorService: http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html

Performers: http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Executors.html

Getting a new fixed stream of threads that processes max. 10 Stream at once:

ExecutorService threadPool = Executors.newFixedThreadPool(10);

      

With the submit method, you submit Callables or Runnables to the pool.



For your use case, you need a process that looks into the queue, if there is a new request, a Callable or Runnable should be created and passed to the thread pool. The pool ensures that the max. 10 threads are executed at once.

This is a very small tutorial: http://www.math.uni-hamburg.de/doc/java/tutorial/essential/threads/group.html

A nice work with thread pools is that the submit method returns a Future object that supports the return types for the threads being executed.

Future: http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html

Hope this helps you solve your problem.

+9


source


I had the same task: I used BlockingQueue

package java.util.concurrent

. I created X worker threads that read one activity from the queue, process it, and prepare the next one. It's simple and it works great.



If you use X = 10 worker threads, your problem is solved.

+2


source


It looks like you need a thread pool executor with corePoolSize = 0 and maximumPoolSize = 10.

http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html

+1


source


If you don't actually have access to create threads and only control access to the queue, the solution might simply be to use an object Semaphore

(see docs page ).

The idea is a global semaphore, which is accessed in the same way as a queue to initialize a number max_threads

(say 10).

Before accessing the queue to process items, a thread first acquires the semaphore, which blocks if a max_threads

number of threads have already started processing items from the queue.

After an item is processed by some thread, that thread should finally release release , thereby allowing more threads to process other items.

Note that getting / releasing a permission must be done using a try-finally block, so that even if an exception is thrown from handling the element, the semaphore remains in a consistent state. The code should look like this:

semaphore.acquire().
try {
    // retrieve item from queue and process it
}
finally {
    semaphore.release();
}

      

0


source







All Articles