5

I have a java application that creates SSL socket with remote hosts. I want to employ threads to hasten the process.

I want the maximum possible utilization that does not affect the program performance. How can I decide the suitable number of threads to use? After running the following line: Runtime.getRuntime().availableProcessors(); I got 4. My processor is an Intel core i7, with 8 GB RAM.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Jury A
  • 19,192
  • 24
  • 69
  • 93
  • 1
    @DarthVader I think the question is: of what size? – assylias Jul 09 '12 at 22:10
  • 2
    @DarthVader: I'm already using pool. But how many threads should I define in the pool ? How can I know which number is suitable for my PC ? – Jury A Jul 09 '12 at 22:12
  • yes, you ave two options, you can create a fixed threadpool with the size of number of cores in your machine. or you can have a thread pool that creates more threads on demand. http://stackoverflow.com/questions/1028707/resonable-number-of-threads-for-thread-pool-in-java – DarthVader Jul 09 '12 at 22:14
  • essentially you shouldnt worry about number of cores vs number of threads u need to create. there are lot of factors that affect thread switch, and while there is thread switch other threads can utilize CPU. so one thread per core doesnt really apply very well. – DarthVader Jul 09 '12 at 22:16
  • Related: http://stackoverflow.com/questions/700072/java-socket-programming-does-not-work-for-10-000-clients – assylias Jul 09 '12 at 22:16
  • Especially if your threads are waiting for IO, pointless to create a thread per core. – DarthVader Jul 09 '12 at 22:16
  • 1
    Also related: http://stackoverflow.com/questions/481970/how-many-threads-is-too-many – dcpomero Jul 09 '12 at 22:18
  • by the way something like this was a interview question from morgan stanley. – DarthVader Jul 09 '12 at 22:19

2 Answers2

8

If you have 4 cores, then in theory you should have exactly four worker threads going at any given time for maximum optimization. Unfortunately what happens in theory never happens in practice. You may have worker threads who, for whatever reason, have significant amounts of downtime. Perhaps they're hitting the web for more data, or reading from a disk, much of which is just waiting and not utilizing the cpu.

Depending on how much waiting you're doing, you'll want to bump up the number. The costs to increasing the number of threads is that you'll have more context switching and competition for resources. The benefits are that you'll have another thread ready to work in case one of the other threads decides it has to break for something.

Your best bet is to set it to something (let's start with 4) and work your way up. Profile your code with each setting, and see if your benchmarks go up or down. You should soon see a pattern and a break-even point.

When it comes to optimization, you can theorize all you want about what should be the fastest, but you won't beat actually running and timing your code to truly answer this question.

corsiKa
  • 81,495
  • 25
  • 153
  • 204
3

As DarthVader said, you can use a ThreadPool (CachedThreadPool). With this construct you don't have to specify a concrete number of threads.

From the oracle site:

  • The newCachedThreadPool method creates an executor with an expandable thread pool. This executor is suitable for applications that launch many short-lived tasks.

Maybe thats what you are looking for. About the number of cores is hard to say. You have 4 hyperthreading cores, at least one core you should leave for your OS. i would say 4-6 Threads.

lhlmgr
  • 2,087
  • 1
  • 22
  • 38