Threads within threads in Java?

前端 未结 4 1030
被撕碎了的回忆
被撕碎了的回忆 2020-12-06 06:13

I am currently thinking about how to design a multithreading system in Java that needs to do some heavy network processing and database storage. The program will launch thre

4条回答
  •  孤街浪徒
    2020-12-06 07:00

    Yes, you can launch as many threads as you want, but that's probably not the best way to go. It's much better to use the non-blocking API's so that you can start execution of some external call and the calling thread can immediately start doing something else without waiting on the socket/database call to come back. Then, when the socket/database call comes back, a callback is triggered to finish that processing.

    Non-blocking I/O can provide far superior CPU utilization since you're just triggering calls and registering callbacks and not having to try to balance the "right" number of concurrent threads which are mostly just sleeping anyways.

    http://www.owlmountain.com/tutorials/NonBlockingIo.htm

    http://www.tensegrity.hellblazer.com/2008/03/non-blocking-jdbc-non-blocking-servlet-apis-and-other-high-mysteries.html

提交回复
热议问题