Introduction

Threads and the concurrency utilities are not sexy subjects, but they are an important part of non-trivial applications. This book introduces you to most of Java’s thread features and concurrency utilities as of Java 8 update 60.

Chapter 1 introduces you to the Thread class and the Runnable interface. You learn how to create Thread and Runnable objects, get and set thread state, start a thread, interrupt a thread, join a thread to another thread, and cause a thread to sleep.

Chapter 2 focuses on synchronization. You learn about problems such as race conditions that cannot be solved without synchronization. You also learn how to create synchronized methods and blocks, and how to use a light version of synchronization that ignores mutual exclusion.

Chapter 3 explores the important topics of waiting and notification. I first review a small API in the Object class that supports these concepts, and then demonstrate this API via a producer/consumer application where one thread produces items that another thread consumes.

Chapter 4 presents three concepts that were not covered in the previous chapters. First, you learn about thread groups, which are not as useful as you might think. Then, you explore thread-local variables. Finally, you learn about the Timer Framework, which simplifies threading for timer tasks.

The previous four chapters covered low-level threading. Chapter 5 switches to a higher level by introducing the concurrency utilities, which can simplify the development of multithreaded applications and improve performance. This chapter then explores executors along with callables and futures.

Chapter 6 focuses on synchronizers (high-level synchronization constructs). You learn about countdown latches (one or more threads wait at a “gate” until another thread opens this gate, at which point these other threads can continue), cyclic barriers, exchangers, semaphores, and phasers.

Chapter 7 explores the Locking Framework, which provides interfaces and classes for locking and waiting for conditions in a manner that’s distinct from an object’s intrinsic lock-based synchronization and Object’s wait/notification mechanism. It offers improvements such as lock polling.

Finally, Chapter 8 presents four subsets of the concurrency utilities that were not covered in Chapters 5 through 7: concurrent collections, atomic variables, the Fork/Join Framework, and Completion Services and Completable Futures.

Each chapter ends with assorted exercises that are designed to help you master the content. Along with long answers and true/false questions, you are often confronted with programming exercises. Appendix A provides the answers and solutions.

Appendix B provides a tutorial on threading in Swing and JavaFX. You learn various fundamentals (such as Swing’s event-dispatching thread) and also explore threading via several interesting example applications (such as a slideshow) that reinforce your understanding of the basics.

Image Note  I briefly use Java 8’s lambda expression feature in some examples, but don’t provide a tutorial on it. You’ll need to look elsewhere for that knowledge.

Thanks for purchasing this book. I hope you find it helpful in understanding threads and the concurrency utilities.

—Jeff Friesen (October 2015)

Image Note  You can download this book’s source code by pointing your web browser to www.apress.com/9781484216996 and clicking the Source Code tab followed by the Download Now link.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.171.253