0

I have 1 thread who sole job is to grab DatagramPackets off of a socket and stick them in a buffer. Another thread works out of that buffer, processing the DatagramPackets. I'd like to have a pool of threads working out of that buffer.

I had thought to use a fixed thread pool to do this. To do so, do I need to create the pool, then submit enough runnables for execution to fill it up? I had hoped for a way to say "this is the thread/runnable that I want you to execute, this is how many I want running, GO!". Is there such a method of doing this? Is something other than a fixed thread pool better suited?

2 Answers 2

2

A fixed thread pool as created by Executors.newFixedThreadPool will work just fine.

The internal semantics of the implementation is such that the thread pool will give preference to creating a new thread until it reaches its preferred size (core pool size), which seems to be what you want.

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

Comments

1

When you use an ExecutorService you submit Runnable jobs to a thread-pool that are run in turn by the threads in the pool. You could do one of the following:

  1. Have each Runnable sit in a loop, dequeueing from a BlockingQueue in a loop to process each packet. That might be easier than all of them synchronizing around your buffer object. Something like:

    public void run() {
       while (!shutdown) {
          packet = packetQueue.take();
          processPacket(packet);
       }
    }
    
  2. Alternatively, you can submit each packet to the thread-pool as a job itself although that might increase your object load. You could process each packet, extract the payload, and create a Runnable wrapper around the payload with the run() method which processes the payload. The contents of the Runnable class would be something like:

    Payload payload;
    public void run() {
       // process packet here
       processPayload(payload);
    }
    

With both mechanisms, I'd choose a fixed thread number that best matches your number of processors and the nature of the processing task. The below example uses the number of processors but you might want to take a couple off for GC or other tasks. You might want to put more on in case the processing blocks on other IO. Only performance testing will tell you what the optimal value is there.

// start a pool that uses the number of threads that there are processors
ExecutorService threadPool = Executors.newFixedThreadPool(
    Runtime.getRuntime().availableProcessors());

5 Comments

#1 seems contrary to the spirit of thread-pool -- dispatching units of work to a pool of up to N threads. Instead, you're spinning up all N threads (where N = max pool size), and each one sits and waits on the queue until work comes in. If done this way, you might as well just create N threads directly and skip the thread pool entirely.
One more thing -- as written, your shutdown strategy for #1 doesn't quite work as the thread may wait forever on the packet queue and never re-inspect "shutdown" and terminate. In this case, I typically use a "poison pill" approach to signal that the thread should terminate.
Really any task that requires threads will be improved with a thread-pool @Jim. Even in case #1.
When you issues a shutdownNow() on the thread-pool that will interrupt() each thread which causes the Thread to throw an InterruptedException. Typically that's what I do. A "poison pill" works as well.
Good point about interruption... as long as the runnable is coded to properly handle interruption. Some times this is impossible to guarantee, particularly if you call out to third-party code which may perform blocking operations.

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.