简体   繁体   中英

Multi-threading with CachedThreadPool

I have server-client communication architecture where there is one server and 150 clients The server-client communication happens via java NIO where all the clients send some or the other data every 10 seconds. Previously we used to queue all the process messages and process all those in a single thread, as the number of clients are more so as the messages, server is not able to process all the messages instantly and there is a delay in processing in turn data loss. So i have thought of implementing CachecThreadPool to process the tasks simultaneously as soon as they come, i have picked CachedThreadPool over FixedThreadPool because the tasks are short lived and many in number, below is the code for that. The thread which receives messages from client calls ProcessorClass.processData(message) as soon as it receives the message.

public class ProcessorClass{

private static final Logger LOGGER = Logger.getLogger(ProcessorClass.class);
static ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();

public static void processData(StringBuffer message) {
    Runnable task = new Runnable() {
        @Override
        public void run() {
            try {
                LOGGER.info("Queue size:"+executor.getQueue().size());
                if (message != null){
                    processMessage(message);
                }
            }
            catch(Exception e) {
                LOGGER.error("Error happened in run() method" + e.getMessage());
            }

        }
    };
    executor.execute(task);
}
public static void processMessage(StringBuffer message){
    // all the processing of message such as DB operations goes here.
}
}

Doubts:

1.How CachedThreadPool stores the message in the queue because i haven't defined any explicitly.
2.Should i chose FixedThreadPool over this?
3.Should i make my processMessage() method synchronized?

All the suggestions and review comments are welcome.

Executors.newCachedThreadPool and Executors.newFixedThreadPool are both have same thread pool implementation just with different parameters. The differences are in their thread minimum, maximum, thread kill time, and queue type.

In FixedThreadPool you can submit any number of tasks to the executor service but max number of threads will be maintained at the level you specified. If you explicitly want to grow the number of threads then this is not the right choice.

CachedThreadPool will not limit the number of thread in the threadpool. It can grow MAX_VALUE of Integer.

How CachedThreadPool stores the message in the queue because i haven't defined any explicitly.

CachedThreadPool does not store your message in the queue. getQueue gets the tasks that you submitted to the queue.

Should i chose FixedThreadPool over this?

CachedThreadPool is more flexible as it will grow and shrink it's number of threads. Problem is that it may grow too much and cause an OOM. FixedThreadPool won't grow or shrink but it's often good enough if you know what you're doing. Hint: do monitoring and profiling to determine how much thread you need.

Should i make my processMessage() method synchronized?

Without knowing the implementation details of the method, we can't advice on whether you need to synchronize it. The method surely need to be thread safe but if you just simply add synchronized on the method, I expect the performance will be even worse than if you just run everything on 1 thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM