...
Wiki Markup |
---|
Read-write locks allow shared state to be accessed by multiple readers or a single writer, but never both. "In practice, read-write locks can improve performance for frequently accessed read-mostly data structures on multiprocessor systems; under other conditions they perform slightly worse than exclusive locks due to their greater complexity." \[[Goetz 06|AA. Java References#Goetz 06]\]. The suitability of read-write locks can be determined by profiling the application. |
Compliant Solution (
...
AtomicBoolean
)
This compliant solution uses the java.util.concurrent.atomic.AtomicBoolean
type to declare the flag
declares flag
as AtomicBoolean
type.
Code Block | ||
---|---|---|
| ||
import java.util.concurrent.atomic.AtomicBoolean;
final class Flag {
private AtomicBoolean flag = new AtomicBoolean(true);
public void toggle() {
boolean temp;
do {
temp = flag.get();
} while(!flag.compareAndSet(temp, !temp));
}
public AtomicBoolean getFlag() {
return flag;
}
}
|
...