18

I'm new to java multi-threaded programming. The question that has came to my mind is that how many threads can I run according to the number of my CPU cores. and if I run threads more than CPU cores will it be an overhead for the machine to run the app. for example when we have a server machine which has a server software that run 2 threads(main thread + developer thread), will it be an overhead for the server when more simultaneous clients make socket connections to the server or not?

Thanks.

Ramin Omrani
  • 3,673
  • 8
  • 34
  • 60

7 Answers7

18

The number of threads a system can execute simultaneously is (of course) identical to the number of cores in the system.

The number of threads that can exist on the system is limited by the available memory (each thread requires a stack and a structure used by the OS to manage the thread), and possibly there is a limitation how many threads the OS allows (this depends on the OS architecture, some OS' may use a fixed size table and once its full no more threads can be created).

Commonly, todays computers can handle hundreds to thousands of threads.

The reason why more threads are used than cores exist in the system is: Most threads will inevitably spend much of their time waiting for some event (example: word processor waiting for user to type on keyboard). The OS manages it that threads that wait in such a manner do not consume CPU time.

Durandal
  • 19,919
  • 4
  • 36
  • 70
6

Idea behind it is don't let your CPU sleep, neither load it too much that it waste most of time in thread switching.

Its helpful to check Tuning the pool size, In IBMs paper

Idea behind is, it depends on the nature of task, if its all in-memory computation tasks you can use N+1 threads (N numbers of cores (included hyper threading)).

Or

we need to do the application profiling and find out waiting time (WT) , service time (ST) for a typical request and approximately N*(1+WT/ST) number of optimal threads we can have, considering 100% utilization of CPU.

Rohit Sachan
  • 1,178
  • 1
  • 8
  • 16
5

That depends on what the threads are doing. The CPU is only able to do X things at once, where X is the number of cores it has. That means X threads at most can be active at any one time - however the other threads can wait their turn and the CPU will process them at appropriate moments.

You should also consider that a lot of the time threads are waiting for a response, or waiting for data to load, or a network message to arrive, etc so are not actually trying to do anything. These idle/waiting threads have very little load on the system.

Tim B
  • 40,716
  • 16
  • 83
  • 128
  • does the server makes one object of itself in main() method per client connection, and if it does, will it be an overhead for the server to run one extra thread per object(I mean what is its effect in networking speed)? thanks sir. – Ramin Omrani Jan 16 '14 at 09:48
  • I don't really understand your comment. There is a thread created to run main. Thread's are only loosely connected to networking speed though. – Tim B Jan 16 '14 at 09:49
3

Don't worry about getting a higher number of threads than CPU cores; that is actually not in your hands, but in OS'.

Assuming the JVM maps your java threads over OS threads (which is fairly normal these days), it depends on the thread management your OS does. There you rely on how smart the kernel implementation is to get performance out of your cores.

What you must keep in mind is that your design must be sustainable. For example, application servers are built on a threadpool full of worker threads. Those threads are awaken in order to serve requests. Do you want a thread for each request? Then you will surely have a problem - requests can arrive in the thousands to the server, and that could be a problem for the kernel to manage. Actually the threadpool size should be limited (between 1 and X and easily changed even in real time), threads should get work from a concurrent queue (java gives you some excellent classes for that) and each one attend requests sequentially.

I hope that being of help

Jorge_B
  • 9,712
  • 2
  • 17
  • 22
2

Having less threads than CPUs can mean you are not using all the CPUs in your system. Having more threads might improve throughput if CPU is your bottleneck.

Having more threads than CPU does introduce an overhead and if CPU is your bottleneck this can hurt performance. However, if network IO, is your bottleneck, this overhead is a price worth paying as it usually allows you to handle many more connections. e.g. You can have 1000 TCP connections with their own threads.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

There doesn't have to be any relation. A computer can have any number of cores; a process can have any number of threads.

There are several different reasons that processes utilize threading, including:

  • Programming abstraction. Dividing up work and assigning each division to a unit of execution (a thread) is a natural approach to many problems. Programming patterns that utilize this approach include the reactor, thread-per-connection, and thread pool patterns. Some, however, view threads as an anti-pattern. The inimitable Alan Cox summed this up well with the quote, "threads are for people who can't program state machines."

  • Blocking I/O. Without threads, blocking I/O halts the whole process. This can be detrimental to both throughput and latency. In a multithreaded process, individual threads may block, waiting on I/O, while other threads make forward progress. Blocking I/O via threads is thus an alternative to asynchronous & non-blocking I/O.

  • Memory savings. Threads provide an efficient way to share memory yet utilize multiple units of execution. In this manner they are an alternative to multiple processes.

  • Parallelism. In machines with multiple processors, threads provide an efficient way to achieve true parallelism. As each thread receives its own virtualized processor and is an independently schedulable entity, multiple threads may run on multiple processors at the same time, improving a system's throughput. To the extent that threads are used to achieve parallelism—that is, there are no more threads than processors—the "threads are for people who can't program state machines" quote does not apply.

The first three bullets utilize threads with no relationship to cores. If you are using threads as a programming abstraction to handle UI elements, for example, you'll have one thread per UI element (or whatever) regardless of whether you have 1 core or 12. Similarly, if you were using threads to perform blocking I/O, you'd scale your thread count with your I/O capacity, not your processing power.

The fourth bullet, however, does relate threads to cores. If the goal of threading is parallelism, then the number of threads should scale linearly with the number of cores. For example, if you double the number of cores in a system, then you would double the number of threads in your application. This is true for cores in the logical sense—that is, including SMT.

When threading is used to achieve parallelism—and this is both a common and the best use of threading—you will often have, say, one or two threads per core. Oftentimes, applications are written so as to dynamically size thread pools off the number of available cores. A single thread per core is ideal, but applications often use a larger multiplier, such as two threads per core, due to bugs and inefficiencies in their code, such as operations that block when none should.

Sushant Verma
  • 899
  • 7
  • 8
0

Best performance will be when number of cores(NOC) equals number of thread (NOT), because if NOT > NOC then processor should switch context or OS will try to do that work, which is expensive enough opperation. But you have to understand that it impossible to have NOC = NOT on Web Servers because you can't predict how much clients will be at the same time. Take a look on load balancing concept to solve this issue in best way.

Divers
  • 9,531
  • 7
  • 45
  • 88
  • 2
    This is a common misunderstanding. If context switches were so bad, wouldn't OS developers just code the OS not to do them? Obviously, unless your OS was developed by an idiot, context switches are only taken where they are beneficial. So avoiding context switches is *not* a sensible reason to reduce the number of threads. You can have 10 threads and do X context switches per second or you can have 100 threads and do the very same X context switches per second. The number of context switches needed is dependent on other design factors. – David Schwartz Jan 16 '14 at 09:54
  • 2
    Context switches may have been expensive ONCE, but certainly not NOW; operating systems are not dumb wrappers around hardware anymore and CPUs are wicked fast. People don't take the time to check if what they know is still current ("Java is slow!"), that is more the problem with these kind of statements IMO. – Gimby Jan 16 '14 at 09:59
  • I dont think that java is slow, versa I think that it is fast enough. And possible OS will try to avoid switching of context and will do it on SW level, but main idea of my post didn't changing because of that, and it absolutly right. – Divers Jan 16 '14 at 10:50
  • Best performance will be when number of cores(NOC) equals number of thread (NOT) - strange, since I have 1311 threads on my box ATM, and it's running just fine. – Martin James Jan 16 '14 at 11:01
  • 1
    @Divers What you say isn't wrong - problem is, its only really true for a very limited type of scenario: purely CPU limited numbercunching. As soon as there is waiting for I/O (or the user) involved, the CPU is utilized much better when there are *more* threads than cores, since not all threads are ready to run at all times. Also a generic "context switches are expensive" statement is meaningless, they are a neccessary evil for multiplexing a single core to multiple tasks. Spending some time for a context switch is still a lot better than busy polling for IO completion. – Durandal Jan 16 '14 at 11:12
  • @Martin James Look at first word (Best). – Divers Jan 16 '14 at 11:17
  • @Durandal I agree with you, but question was "if I run threads more than CPU cores will it be an overhead for the machine", and answer is "yes if they need proccessor time in the same time". – Divers Jan 16 '14 at 11:22
  • @Divers - but it's still either wrong or massively misleading. My uTorrent app currently has 58 threads - you think it would work better with 8? – Martin James Jan 16 '14 at 14:37
  • @Martin James E.g we have 8 threads that takes 100% of processor time. If we will create 16 threads each of which will take 50% of 1 core it would be less efficient, because we will spend time on switching between them. 1 core = 1 thread at the period of time. – Divers Jan 16 '14 at 19:25