Appendix D. Understanding Java threads

 

In this appendix, we’ll discuss the basics of threads in a Java app. A thread is an independent sequential set of instructions your app runs. Operations on a given thread run concurrently with those on other threads. Modern Java applications often depend on multiple threads, making it inevitable to encounter situations where you need to investigate why certain threads are not behaving as expected or why they struggle to cooperate with other threads. That’s why you’ll find threads in several discussions throughout this book (especially chapters 7 to 9, but also here and there in the first half of the book when we discuss debugging). To properly understand these discussions, you need to know some basics about threads. This appendix teaches you those elements that are essential for understanding other discussions we have throughout the book.

D.1 What is a thread?

D.2 A thread’s life cycle

D.3 Synchronizing threads

D.3.1 Synchronized blocks

D.3.2 Using wait(), notify(), and notifyAll()

D.3.3 Joining threads

D.3.4 Blocking threads for a defined time

D.3.5 Synchronizing threads with blocking objects

D.4 Common issues in multithreaded architectures

D.4.1 Race conditions

D.4.2 Deadlocks

D.4.3 Livelocks

D.4.4 Starvation

D.5 Further reading