[concurrency-interest] Blocking vs. non-blocking

Dennis Sosnoski dms at sosnoski.com
Fri Jun 13 19:35:48 EDT 2014


I'm writing an article where I'm discussing both blocking waits and 
non-blocking callbacks for handling events. As I see it, there are two 
main reasons for preferring non-blocking:

1. Threads are expensive resources (limited to on the order of 10000 per 
JVM), and tying one up just waiting for an event completion is a waste 
of this resource
2. Thread switching adds substantial overhead to the application

Are there any other good reasons I'm missing?

On the thread switching issue, I tried a simple timing test where I 
create some number of threads and have them take turns incrementing a 
value, each one passing control off to the next after an increment. For 
a total of 4096*100 increments here's what I got on my 4-core AMD Linux 
desktop running Java 7:

Took 44 ms. with 1 threads
Took 3805 ms. with 2 threads
Took 6172 ms. with 4 threads
Took 6185 ms. with 8 threads
Took 6437 ms. with 16 threads
Took 6831 ms. with 32 threads
Took 6756 ms. with 64 threads
Took 6511 ms. with 128 threads
Took 6975 ms. with 256 threads
Took 7264 ms. with 512 threads
Took 7185 ms. with 1024 threads
Took 6826 ms. with 2048 threads
Took 7639 ms. with 4096 threads

So a big drop in performance going from one thread to two, and again 
from 2 to 4, but after than just a slowly increasing trend. That's about 
19 microseconds per switch with 4096 threads, about half that time for 
just 2 threads. Do these results make sense to others?

Thanks,

   - Dennis



More information about the Concurrency-interest mailing list