In a few places in this book, we’ve referred to performance characteristics of thread-related programming. We’ve glossed over a lot of that information; in this chapter, we’ll look at these performance issues in more depth. In particular, we’ll look at thread creation performance, the performance advantages of using a thread pool, and the real costs of synchronization. However, we’ll start with an overview of factors that affect Java performance.
Most developers are concerned about the performance of their program. Even though there are many programs for which performance doesn’t really matter, no one wants to write a badly performing program. And there are many more programs for which performance is crucial.
Performance, however, is not the most important aspect in developing good programs. We’ve frequently met developers who allow their concerns about performance to complicate their program development: for example, believing that synchronization is inherently expensive, they may spend days attempting to write a class that doesn’t need synchronization. The resulting code is complex, difficult to maintain, and more prone to bugs than a simpler (in this case, synchronized) version.
Without any prior knowledge of a program’s behavior, this is counterproductive. Developer time is wasted, and support costs are increased. This observation leads us to our first rule of performance: premature optimization is the root of much evil.[1