Previous | Next | Trail Map | Writing Java Programs | Threads of Control


Multithreaded Programs

Synchronizing Threads

Often, threads need to share data. For example, suppose you have a thread that writes data to a file while, at the same time, another thread is reading data from that same file. When your threads share information, you need to synchronize the threads to get the desired results. The next section shows you how to synchronize threads in your Java programs.

Fairness, Starvation, and Deadlock

If you write a program in which several concurrent threads are competing for resources, you must take precautions to ensure fairness. A system is fair when each thread gets enough access to limited resource to make reasonable progress. A fair system prevents starvation and deadlock. Starvation occurs when one or more threads in your program is blocked from gaining access to a resource and thus cannot make progress. Deadlock is the ultimate form of starvation; it occurs when two or more threads are waiting on a condition that cannot be satisfied. Deadlock most often occurs when two (or more) threads are each waiting for the other(s) to do something.

Deadlock and the Dining Philosophers uses the dining philosophers problem to illustrate deadlock. It also discusses ways you can prevent deadlock.


Previous | Next | Trail Map | Writing Java Programs | Threads of Control