How many threads should I use in my Java program?

前端 未结 7 1311
日久生厌
日久生厌 2020-12-09 18:03

I recently inherited a small Java program that takes information from a large database, does some processing and produces a detailed image regarding the information. The ori

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 18:44

    On the one hand, you'd like to think Threads == CPU/Cores makes perfect sense. Why have a thread if there's nothing to run it?

    The detail boils down to "what are the threads doing". A thread that's idle waiting for a network packet or a disk block is CPU time wasted.

    If your threads are CPU heavy, then a 1:1 correlation makes some sense. If you have a single "read the DB" thread that feeds the other threads, and a single "Dump the data" thread and pulls data from the CPU threads and create output, those two could most likely easily share a CPU while the CPU heavy threads keep churning away.

    The real answer, as with all sorts of things, is to measure it. Since the number is configurable (apparently), configure it! Run it with 1:1 threads to CPUs, 2:1, 1.5:1, whatever, and time the results. Fast one wins.

提交回复
热议问题