I posted this question asking why 100000 run
() calls is faster compred to 100000 start
() as i found out that despite multi threading, 100000 start
would actually take longer than 10000 run
calls because of the thread management.
Actually, i was trying to spawn 100000 threads to simulate a load on an EJB method i wish to test, and it seems not possible this way. Is there a way which i could achieve this? Or is it that i would need to have multiple machines in order to achieve that load.
Is it true that if i have a quad core pc, i should only spawn at most 4 threads at a time to prevent too heavy context switching because at any one time 4 threads would be run?
If you have 4 cores which support hyper threading, you can only actually load 8 threads at once. You can start more threads than this, however only 8 can be active at any one time. This is a limitation of the hardware you are using.
I very much doubt you need to run 10K or 100K threads to test any system. Most systems can be saturated with work with just one thread (or a very small number) and I suspect your EJB is no exception.
You cannot test a method is thread safe via brute force testing. You can only determine this by reading the code.
You might find this article interesting Java: What is the limit to the number of threads you can create?
100000 is definitely too much. If your thread is CPU intensive then only 1 thread per CPU/core should be enough. On the other hand, if your thread is not CPU intensive, you can have more than 1 thread per CPU/core. You should conduct more test to find out the optimum number of thread.