简体   繁体   中英

Using CompletableFuture to exit from main thread

I have a JMS queue and I have written a consumer for this JMS queue. I'm planning to remove all the logics inside this Consumer method and write a CompletableFuture and run all the logics through the CompletableFuture and let the main consumer thread exit. What my idea about this approach is that since all the logics will be executed asynchronously, I will be able to release the main thread and free some CPU and space. Could you please let me know whether my thinking is correct. Thank you so much for your help.

Using a concurrency mechanism to process data from a JMS queue is a very common pattern and is often a good idea. It won't necessarily save CPU since the amount of work is the same, but it will allow you to process more messages more quickly if you execute your processing logic as part of a thread pool.

Usually, your Consumer would be responsible for taking the message from the queue, creating an object to do the processing ( CompletableFuture or other), and submitting it to a thread pool (see Executors ).

Without knowing the details the general answer is Yes. Processing data from a queue using concurrency mechanisms is a common pattern and a good practice, however there are of course tons of caveats and design choices that depend on your specific requirements so buyer beware.

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