Recently, I came across an interesting presentation by Paul Tyma (Senior Engineer at Google / CEO at ManyBrain Inc), regarding the performance of Java NIO vs classic IO APIs. The presentation was titled as ‘Thousands of Threads and Blocking I/O – The old way to write Java Servers is New again (and way better)‘. The title itself sounds downright interesting, and it was worth reading (learned a lot from it). I highly recommend this presentation to any Java programmer. You can download the slides from here.
However, the reason for this blog post is not the presentation itself. Rather, this blog post is about a question that Pual raises (tagging it as ‘Interview question’), that many people will answer wrong, because the answer that comes to mind looking at the surface is misleading.
What’s harder, synchronizing 2 threads or synchronizing 1000 threads?
Obviously, synchronizing 2 threads, compared to 1000 threads should be easy ! right ? err…. wrong. While the question might look mundane, it is based on the roots of concurrency theory. If you are to correctly synchronize even 2 threads, it’s not an easy job. But if you get it right, then it doesn’t matter whether it is 2,3,4,…1000 or n threads. It supports concurrency. Yes, there will be other issues (you might have a semaphore which is initialized with 2, which you will need to change), but your solution will support 1000 threads, if it can correctly support 2.
Yes, you can easily get a concurrent application up and running without issues with just 2 threads, and you are bound to face more issues when you are running on 1000 threads. But those issues that you face with 1000 threads are just there even in the 2 threaded application, bidding time till the perfect moment comes to crash your application. You will not find it running once, twice, many times (or even never !), but the loop hole is there, and it might popup at some point later, bringing down the application. But, when you run it on 1000 threads, chances of you hitting those loop holes are much higher.
And that makes getting your application running for the first time with 1000 threads harder. But, writing a properly synchronized application with 2 threads is same as making it sync’d for 1000 threads.
So the answer to the question is, ‘There’s no difference. It is the same.’
UPDATE 08/08/2010 : Fixed the link to original presentation