Information Technology Reference
In-Depth Information
The pitfall is that when extending the Thread class (or writing a new class that im-
plements Runnable) programmers can be tempted to include not only the thread's
main loop but also state that will be shared across multiple threads, blurring the
lines between the threads and the shared objects. This is almost always confus-
ing.
For example, for a blocking bounded queue, rather than defining two classes,
BBQ for the shared queue and WorkerThread for the threads, programmers are
sometimes tempted to combine the two into a single class—for example, a queue
with an associated worker thread. If this sounds confusing, it is, but it is a pitfall
we frequently see among students.
The solution is simple—always make sure threads and shared objects are defined
in separate classes. State that can be accessed by multiple threads, locks, and
condition variables should never appear in any Java class that extends Thread or
implements Runnable.
5.6.8
Example: Readers/writers
Multithreaded programming is an important skill, and we anticipate that almost
everyone who reads this topic will be frequently called on to write multithreaded
programs. This section and the next walk through two substantial examples that
illustrate how to implement shared objects.
First, we will solve the classic readers/writers problem. In this problem,
we have a database that can have records that can be read and written. To
maximize performance, we will allow multiple threads to simultaneously read
a record. However, for correctness, if any thread is writing a record, no other
thread may simultaneously read or write that record.
Thus, we need to generalize our mutual exclusion Lock into readers-writers
lock. We will do this by implementing a new kind of a new kind of shared object,
RWLock , to guard access to each record and enforce these rules. The RWLock will
be implemented using our standard synchronization building blocks:
mutual
exclusion locks and condition variables.
Then, a thread that wants to read a record will proceed as follows:
rwLock->startRead();
//Readdatabaseentry
rwLock->doneRead();
Similarly, a thread that wants to write a record will do the following:
rwLock->startWrite();
//Writedatabaseentry
rwLock->doneWrite();
Search WWH ::




Custom Search