4

I couldn't find an example of this specific case using the latest JAVA concurrent routines.

I plan to use threads to process items from an open queue which may contain 0 to thousands requests. I want to restrict so at at any given time there be no less than 0 and no more than say 10 threads handling queue items.

Is there a Java concurrent process geared towards this specific type of case?

2
  • check out the java.util.concurrent package. if there's not one that will work for you right out of the box, then you could always wrap it... Commented Feb 1, 2013 at 18:58
  • "there be no less than 0", do you mean you don't want a negative number of thread? ;) Commented Feb 1, 2013 at 19:48

4 Answers 4

10

I think a 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

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

Getting a new Thread fixed thread-pool that processes max. 10 Thread at once :

ExecutorService threadPool = Executors.newFixedThreadPool(10);

With the submit Method you pass 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 has to be created and passed to the thread-pool. The pool ensures that 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 thing working with thread-pools is that the submit method returns a Future object, which supports return types for the executed threads.

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

I hope this helps you to solve your problem.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 The fixed thread pool also has a queue so you don't need to add one.
Yes, you are right, I didn't think that way, just thought the queue of requests still exists.
3

I had the same task: I used the BlockingQueue of the java.util.concurrent package. I created X worker threads which reads one action from the queue, processes it, and when ready take the next. This is simple, and it works fine.

If you use X = 10 Worker threads, then your task is solved.

Comments

1

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

Comments

0

If you don't actually have access to the creation of the threads, and you only manage the queue access, a solution could simply be to use a Semaphore object (see the docs page).

The idea is for a global semaphore, accessed in the same way the queue is accessed, to be initialized to the number max_threads (say, 10).

Before accessing the queue for item processing, a thread would first acquire a permit from the semaphore, which would block if a max_threads number of threads had already started processing items from the queue.

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

Note that the acquiring/releasing of a permit should be done using a try-finally block, so that even if some exception is thrown from the item processing, 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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.