Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
java.awt.Toolkit is an example of a singleton.) Along the way, I'll also look at critical sections, or blocks of code -- as compared to objects -- that can be locked.I'll finish up with a completely unrelated topic: reader/writer locks, which give you efficient, thread-safe access to read/write resources such as data structures and files. Reader/writer locks are simple enough to implement that I didn't want to devote an entire column to them, but they're essential in any multithreaded program that performs I/O operations, so I wanted to include them in the present series of articles. Reader/writer locks combined with the various semaphores and locks I've presented in previous installments of this series comprise a reasonably complete toolkit for solving thread-related synchronization problems.
So far in this series I've been concentrating on the monitor -- a means of locking an entire object while a body of code is being executed. The other essential sort of lock you should be aware of is the critical section. Critical sections are essential in implementing one-time initialization code when that code can be accessed from multiple threads.
A critical section is a chunk of code that can be executed by only one thread at a time. Compare this notion with a normal
synchronized code block -- a monitor -- which is basically an exclusion semaphore that guards an entire object. Several threads can simultaneously execute a synchronized method, but only if the objects that are receiving the associated messages are different. In a critical section, the code
itself is locked, not the object. Only one thread can be in the critical section at a time, even if the receiving objects
are different. The mutex that guards a monitor is an object-level mutex; the mutex that guards a critical section is effectively
a class-level mutex. Think of it this way: the code is defined in the class, not the object, so when you're locking the code
itself, you're locking the entire class of objects. (By the way, I've seen authors get this wrong in print when they call
a block of code inside a nonstatic method a "critical section." A block of code in a nonstatic method is part of the object's
monitor; it is not a critical section.)
Static members
In Java, the notion of a critical section is closely tied to that of a static member, so let's start there. Java, like all
OO languages, supports two categories of fields and methods:
| Class variables: | variables that control the state of all objects within a class. |
| Instance variables: | variables that control the state of a single object within a class. |
A class variable is implemented in Java by placing a static keyword before its definition.