Chapter 8. Concurrency

Not that long ago, it wasn't unusual for programs to have a single thread of execution, even if they were running on a multithreading system. Even today, code you write for an application or Web server tends to be single-threaded, even if the server itself is multithreaded. Why? Because multithreaded programming (often referred to as concurrency) is hard to do correctly, even when the programming language directly supports it. The incorrect use of threads can easily halt your program's execution or corrupt its data.

However, if you're writing applications with a graphical user interface or that perform lengthy I/O operations, chances are good that you'll need to do some of the work using a background thread or two. Even JavaScript programmers doing AJAX-style programming encounter threading issues — even though neither the language nor the standard libraries explicitly support multithreading — because the Web server responses are processed asynchronously, and hence the JavaScript that runs to process the response possibly accesses data used by other parts of the application. That's why good programmers take the time to learn how to write multithreaded programs correctly.

Get Programming Interviews Exposed: Secrets to Landing Your Next Job, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.