开发者

Considerations when using more threads than hardware threads?

开发者 https://www.devze.com 2023-04-06 22:02 出处:网络
I have a brand new laptop with a i7 2630qm CPU (4 cores, 8 threads) 开发者_开发知识库and I\'m excited about tapping into the power of a multi-core processors.

I have a brand new laptop with a i7 2630qm CPU (4 cores, 8 threads) 开发者_开发知识库and I'm excited about tapping into the power of a multi-core processors.

After reading about threads, I realized I had a few areas of confusion in regards to how one writes the most efficient code.

I have about 200 tables, each with about 40,000 records. I plan on pulling each table in its entirety into a multidimensional array inside the java program, and running simulations against the array.

Obviously, it would be one array per thread, but what I’m wondering is: Would I receive any benefits from creating all the (200) threads simultaneously, RAM permitting, or writing code that just switches out arrays between 8 threads?

I mean, from what I understand, my i7 only has 8 hardware threads.


I would look into Thread Pools as they might help address the basic desire to limit the number of concurrent threads.

In my experience (in Windows) more threads (about 2x the core threads) allows code with independent CPU-bound threads to run faster: that is, the program can steal more priority that the Operating System might give to other processes ;-) Setting the thread priority can help as well and, of course, IO-bound threads are an entirely different story.

Remember your code does not run in isolation and the operating system is ultimately responsible for the thread scheduling:

The only way to know "for certain" is to try different things and run performance tests.

The results may be surprising and are influenced by many factors including, but not limited to, JVM/OS, algorithm, thread contention, and user-space duty cycle (IO operations end up in kernel-land). Also keep in mind that different parts of the program may react entirely differently and it might not even make sense to thread some parts (such as reading in the initial data), depending on where the bottleneck(s) materialize(s) and -- not to be disregarded -- program complexity.

(Using a thread pool can easily allow adjusting of the number of concurrent threads allowed, which is one reason why I suggested it.)

Happy mutli-threaded coding.


What are you planning to do in each thread -- query the database, or process data? If it's the former, multiple threads won't help as you'll be I/O bound. If it's the latter, careful design could indeed give you substantial speedup using multiple threads.


The answer is a definitive "it depends". Having more threads then your hardware supports may speed up your program. For example, imagine you have 1 thread holding a lock for I/O to the database, while 7 other threads wait for it to finish. Having more threads doing other work in the meantime would certainly speed up this program.

Since your program appears to do a lot of I/O with database interactions, as well as a lot of CPU intensive work once the data has been collected, you may well find that having more then 8 threads makes your program run faster.

As others have suggested, a little trial-and-error is probably in order. Good luck.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号