Taming Java Threads
Understanding threads is essential to writing real-world Java programs, All Java applications that incorporate a graphical user interface are multithreaded, for example, and ignoring this fact can make your programs unusable. Support for multithreading must be incorporated into your program from the start-it's very difficult to retrofit an existing program to make it "thread safe."Though Java incorporates multithreading into the language, Java's threading model is primitive at best and full of flaws that render it difficult to use in real programs. Moreover, Java threads are the one part of the language that are platform dependant, so writing a platform-independent threaded program requires some knowledge of how threads are implemented in various underlying operating systems The situation is made worse by the fact that multithreaded programming is inherently difficult, even for programmers with considerable threading experience. Finally, Java's threading model is not at all object oriented.
This class addresses all of the foregoing issues, showing you how to use threads effectively in a real-world program. The class begins with a discussion of the various ways that threading can work. We then look at multithreading in the Java environment and at how to program effectively with threads, with a focus on "traps and pitfalls," such as deadlock. The class moves on to present a production-quality library of threading classes that augment the built-in threading support sufficiently that it is possible to write real programs. Finally, we look at object-oriented threading models and how to implement them in Java.
On completion, you should know how to build robust multithreaded applications in Java. You will understand how threading works and will be able to recognize the flaws in the Java threading model, and thereby be able to program around them. Your multithreaded programs will be easier to design, build, and debug as a consequence.
Prerequisites
The course is fast paced and technically rigorous. It is intended as a follow-on to Java: An Intensive Introduction insofar as a basic familiarity with the Java language is assumed. The class is geared toward experienced professional programmers who are learning Java. Participants should have written at least a few small Java programs and should understand object-oriented concepts such as derivation and function overriding. This is not an introductory programming class and is not suitable for individuals learning Java as their first programming language.Course Outline
- Books on Threading
- Threading Architectures
- Native Threads vs. In-Process Threads
- The SUN Multi-processing Model
- Specific Java Considerations
- Thread Support in Java
- Creating Threads (Thread and Runnable)
- Thread States:
- Threads and Garbage Collection
- Inter-thread communication
- Priorities
- Daemons
- Thread Groups
- Synchronization and Monitors
- Object-Level Synchronization:
- Method-Level Synchronization:
- Synchronizing static Methods
- Synchronizing static Fields
- Wait() and Notify()
- Classic Problems
- Lock Overhead
- Getting the wrong Thread
- Race Conditions
- Deadlock
- Deadlock, Example 1
- Deadlock, Example 2: The same problem, obscured
- Do Not synchronize run()
- Problems with wait() and notify()
- Deadlock Through Circular Waits
- Unintended Race Conditions
- Nested-Monitor Lockout
- Starvation
- Synchronization and Inner Classes
- Bean Synchronization
- Thread-Specific Data
- Some Thread-Based Utilities
- Generic Semaphore Support (Lock_manager)
- The Mutual-Exclusion Semaphore (Mutex)
- Condition Variables
- The Dijkstra Counting Semaphore
- Read/Write Locks
- Singletons and Critical Sections
- The Singleton Pattern
- Singleton Creation (Critical Sections)
- Getting Around The JDK 1.1 Class-Unloading Bug
- Timers (com.holub.asynch.Alarm)
- Object-Oriented Multithreading
- Simple Asynchronous Messages
- The One-Method-Per-Thread Model
- A LinkedList-based Blocking Queue
- Thread Pools
- A thread-pool based socket server
- Synchronous Notification Dispatching
- Active Objects
- An Active-object Dispatcher
- Thread-safe Console Output
- Synchronous Responses
