Name: _____________________________ 6 Digit StudentID: ___ ___ ___ ___ ___ ___

Worksheet 18: Mutual Exclusion

1 Given the partial definition of MutableInt and parallel program below:

public class MutableInt {
    private int value;
    public MutableInt() {
        value = 0;
    }
    public void add(int operand) {
        value += operand;
    }
    public int intValue() {
        return value;
    }
}
MutableInt total = new MutableInt();
Future<Void> f = void_fork(() -> {
    synchronized(total) {
        total.add(11);
    }
});
total.add(10);
join(f);
System.out.println(total.intValue());

1a) Is there a data race? _______

1b) If so, encircle all of the lines which contribute to the data race.

String[] texts = new String[1];
texts[0] = "";
Future<Void> a = void_fork(() -> {
    synchronized(texts) {
        texts[0] = texts[0] + "A";
    }
});
Future<Void> b = void_fork(() -> {
    synchronized(texts) {
        texts[0] = texts[0] + "B";
    }
});
Future<Void> c = void_fork(() -> {
    texts[0] = texts[0] + "C";
});
join(a, b, c);
System.out.println(s[0]);

2a) What can run in parallel with:

texts[0] = texts[0] + "A"? __________________
texts[0] = texts[0] + "B"? __________________
texts[0] = texts[0] + "C"? __________________

2b) How many different ways can the reads and writes of texts[0] play out?

 

2c) What are all the different String lengths texts[0] can be when it is printed?

 

2d) What are all the different String values texts[0] can be when it is printed?

 

Post Lecture

Synthesize today's class session

 

 

What is unclear?