How many threads should/can be run?
will be of no use in single-core systems unless some threads had to
wait for file/network I/O, DB query completions, UI input or they had
to wait for other threads to produce/consume.
is because in a single core system, only one thread is active at a
true parallelism is achievable because there multiple threads will
really run simultaneously. However, there also, the number of
parallel executions depends upon the number of cores. So a quad core
system will run only 8 parallel threads.
threads involve waiting and the system on which they run is
multi-core, what is the maximum number of threads which can run on a
thread is given some specific amount of stack-size to execute (heap
is shared by threads of same process, so that is not a constraint).
That stack-size per thread is configurable and the default is
different in different systems. The default stack-size per thread
varies from 1 MB to 8 MB. If the machine on which the threads are
running is a 32-bit machine, then it can access a memory space of 2^31=2GB.
the maximum no of threads which can be created = 2GB/8MB ~ 256
you try to create more threads than this, then out-of-memory error
number of threads = total virtual memory / (stack size*1024*1024)
linux systems, these values can be checked as:
file size (blocks, -c) 0
seg size (kbytes, -d) unlimited
size (blocks, -f) unlimited
locked memory (kbytes, -l) unlimited
memory size (kbytes, -m) unlimited
files (-n) 1024
size (512 bytes, -p) 8
size (kbytes, -s) 8192
time (seconds, -t) unlimited
user processes (-u) unlimited
memory (kbytes, -v) unlimited
of a thread
“ulimit” command, for example ulimit -s 192 will reduce
the stack size from 8MB to 192kb.
C applications can call pthread_attr_setstacksize().
Java applications can use “-Xss” to set the stack size.
Remember that switching
thread context is quite expensive and so adding threads to a system
does not linearly scale the performance of the system.
maximum no of threads depends upon the maximum no of open files. As
seen above in the command ‘ulimit -a’, system places a
limit on the max no of open files too. If each of your threads opens
a file, then this limit can also kill the application.
restrictions may be placed by other systems like maximum no of
connections imposed by a database etc.