3

I know I can specify the Fixed Thread Pool Size using

ExecutorService executor = Executors.newFixedThreadPool(10);

I can add runnable objects into the executor and they execute whenever a thread is free in the pool

executor.execute(Obj);

I want to limit the no of objects to be added to the executor service i.e. if I have 100 runnable objects, the Thread Pool Size is 10 and only 20 must be added to the ExecutorService and rest must be rejected.

I want to create a fixed size waiting list for the executor so that instead of adding all 100 objects and keeping them in wait state, it must just keep a fixed no of items in waiting state

I went through the Executor and ExecutorService API, but didn't find any such thing, just wanted to know if this is possible ?

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • 3
    you just wanted to know if its possible? Right? Its possible!! Google little hard. Hint. `BlockingQueue`, `ThreadPoolExecutor`. – sakura Feb 26 '14 at 13:05
  • 1
    Add *rejection policy* to the list of googlable keywords. – Marko Topolnik Feb 26 '14 at 13:19
  • Related http://stackoverflow.com/questions/4521983/java-executorservice-that-blocks-on-submission-after-a-certain-queue-size?rq=1 – aalku Feb 26 '14 at 15:39

3 Answers3

2

Look into ThreadPoolExecutor's constructors. You may provide a bounded queue and a rejection policy to your executor. The rejection policy tells the executor what to do when you try to submit more tasks than it can handle.

Example:

ExecutorService executor =
    new ThreadPoolExecutor(N_THREADS, N_THREADS, 0L, TimeUnit.MILLISECONDS,
        new LinkedBlockingQueue<Runnable>(CAPACITY),
        new ThreadPoolExecutor.CallerRunsPolicy());
afsantos
  • 5,178
  • 4
  • 30
  • 54
1

All of the Executor service - single, fixed, cached thread pools are backed by generic executor ThreadPoolExecutor.

You can override execute method as follows:

class FiniteQueuedExecutorService extends ThreadPoolExecutor{
    int limitQueueSize=Integer.MAX_VALUE;

    //matching constructors here

    @Override
    public void execute(Runnable command) {
        if(getQueue().size()>limitQueueSize)
            throw new RuntimeException("Too Many enqueued runnables");
        super.execute(command);
    }
}

Note: you will have to create new static factory like Executors to create instances of these.

Amit G
  • 2,293
  • 3
  • 24
  • 44
0

Try to wrap your Executor or ExecutorService with:

public class ExecutorCountWrapper implements Executor {
    private final Executor executor;
    private final AtomicInteger count = new AtomicInteger();

    public ExecutorCountWrapper(Executor executor) {
        this.executor = executor;
    }

    @Override
    public void execute(final Runnable command) {
        count.incrementAndGet();
        executor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    command.run();
                } finally {
                    count.decrementAndGet();
                }                
            }
        });        
    }

    public int getTaskCount() {
        return count.get();
    }
}

Now you can check how many tasks wait for execution, and submit more or not.

m-szalik
  • 3,546
  • 1
  • 21
  • 28