Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Tolls

Thread: how many threads in a thread pool?

  1. #1
    johnmerlino is offline Member
    Join Date
    May 2014
    Posts
    56
    Rep Power
    0

    Default how many threads in a thread pool?

    I am writing a udp socket server. It can have 100,000+ concurrent connections. Initially I was planning on spawning a new thread for each connection but realized that may cause the system to crash, as the system runs Ubuntu and has 8gb of memory and a 4 core Intel(R) Xeon(R) CPU X3430 @ 2.40GHz. I want to maximize the use of threads as much as possible. I am using Java 8 and plan to use the ExecutorService as follows:

    Java Code:
     public void run() {
    	
    	ExecutorService executor = Executors.newFixedThreadPool(100);
    	
        while (listening) {
        	try {
                byte[] buf = new byte[256];
    
                DatagramPacket packet = new DatagramPacket(buf, buf.length);
                // wait until datagram received on socket
                socket.receive(packet);
                
                executor.execute(new Responder(socket, packet));                
            } catch (IOException e) {
                e.printStackTrace();
            }
        }		
    }
    As you can see, for each new packet that arrives, I retrieve a thread from the thread pool. But once we exceed 100 concurrent packets coming in, we have a big problem. The new data is going to have to wait until a thread is free. Each of these threads will decode the packet and then insert the result into a postgresql database sitting on another server within the LAN. The data has time-sensitive information and must be inserted into the database in a matter of seconds after it arrives to socket server. How high should I increase the thread pool in order to ensure data doesn't have to sit and wait for available threads?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: how many threads in a thread pool?

    How high should I increase the thread pool in order to ensure data doesn't have to sit and wait for available threads?
    Sounds like a Queuing Theory problem. You need to know at least the arrival rate and distribution, the processing time to handle a request, the maximum accepted delay and probably some other things to answer that question.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: how many threads in a thread pool?

    As I tried to explain already: increasing the number of threads is very likely to not be the solution because it makes the overall processing even slower.

    If you encounter io problems, then you have to find the bottleneck:
    - Is it the server your code running on?
    - Is it the network to the database server?
    - Is it the datsbase server?

    What we might do is optimizing areas. But for that we need to see details. But you must do the analysis of the whole else you end up tuning something that might not be the problem.

    Konrad

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: how many threads in a thread pool?

    Look at Akka.
    It may involve a fair bit of learning on your part, but it will save you a lot of time attempting to produce essentially the same thing.

    But you will need to sit down and think about what your code does, and where the heavy lifting (or blocking threads) are, so you can split the effort up into the correct collection of Actors.

    I will point out that trying to "ensure data doesn't have to sit and wait for available threads" might be an odd goal. After all what is, practically, the difference between a connection having to wait for an available thread, and a connection having to wait for its thread to get some execution time? If, once processing has finished, the total time is the same, it's pretty irrelevant where the delay comes in.
    kneitzel likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Regarding thread creation or thread pool for 100 tasks
    By ashishgupta_mca in forum Threads and Synchronization
    Replies: 2
    Last Post: 05-21-2012, 07:57 AM
  2. Replies: 2
    Last Post: 07-19-2011, 06:01 AM
  3. need help with thread pool
    By crank6015 in forum New To Java
    Replies: 1
    Last Post: 04-03-2011, 02:47 PM
  4. Thread Count Extends Beyond Pool
    By jvermast in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-01-2010, 07:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •