What’s harder, synchronizing 2 threads or synchronizing 1000 threads?

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

11 comments

  1. Absolutely right, but if people have an application supposed to execute on no more than 2 threads, then they’ll think and design it like that and above all test it like that, which may leave the loopholes behind waiting to get bombarded in future in real systems, so special attention needs to be paid in such applications to avoid current as well as implications coming in future.

    Nice topic.

    –Deepak

    1. Thanks for the reply Deepak. Yes, you are correct. Special attention is definitely needed before such an application is applied for many number of threads.

  2. Hii yohan, nice topic with absolute correct answer, but I would like to add something to deepak’s answer. If we are going for a multi-threaded applications we can’t go for best case scenarios behaves under optimal conditions.Then there is no point of going for such application development. So mulit-threaded applications always have to foreseen the worse case situation And as yohan mentioned above if the system has properly synchronized, there’s no difference. It is the same with synchronizing 2 threads or synchronizing 1000 threads

  3. While I generally agree with your answer, I think you are also overlooking the performance aspects.

    If you have a requirement that the application has to perform well, and if there is a lot of contention on some data structures, then it might be easier to achieve better performance with 2 threads than 1000.

    When you have 1000 threads all trying to access some highly contended piece of data, you might be forced to use more complex locking schemes than if you had just 2 threads (e.g. more fine-grained locking, RCU, or even lockless data structures).

    1. Hi Ricardo,

      Thanks for your reply.

      Actually, yes, performance and other aspects were not a consideration for this post. What I was focusing on is the accuracy of synchronization. If we look at aspects such as performance, as you have correctly mentioned, it depends on lots of factors such as the data structures, environment in which the application is running, and so on.

      Yes, performance-wise there are more pressing concerns when porting an application from 2 threads to 1000 threads. But if we look at the difficulty of getting a 2 threads application to be in proper sync, compared to a 1000 threaded application, it’s theoretically bears the same difficulty.

Leave a Reply

Your email address will not be published. Required fields are marked *