undefined

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Reader-Writer Locks - Part II

In order to return the correct value, we may want to make the cache a mutually exclusive resource by making the following changes to the program:

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
class Writer extends Producer implements Runnable {
public void run() {
synchronized (Shared.cache) {
int[] referenceToValue = Shared.cache.get("key");
for (int i = 0; i < referenceToValue.length; i++) {
// Update value in-place; sum will add up to length
referenceToValue[i] = produce();
}
}
}
}
class Reader extends Consumer implements Runnable {
public void run() {
synchronized (Shared.cache) {
consume(Shared.cache.get("key"));
}
}
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content