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

Worksheet 02: Blackjack Data Races

Version A

int[] cards = new int[2];
void_fork(() -> {
    cards[0] = 11;
});
cards[1] = 10;
int total = cards[0] + cards[1];
System.out.println(cards[0] + cards[1]);

Version B

int[] cards = new int[1];
Future<Void> future = void_fork(() -> {
    cards[0] += 11;
});
cards[0] += 10;
join(future);
int total = cards[0];
System.out.println(total);

Version C

int[] cards = new int[1];
void_fork(() -> {
    cards[0] += 11;
});
cards[0] += 10;
int total = cards[0];
System.out.println(total);

Version D

int[] cards = new int[2];
Future<Void> cardAFuture = void_fork(() -> {
    cards[0] = 11;
});
cards[1] = 10;
join(cardAFuture);
int total = cards[0] + cards[1];
System.out.println(total);

Version E

Future<Integer> cardAFuture = fork(() -> {
    return 11;
});
int cardB = 10;
int cardA = join(cardAFuture);
int total = cardA + cardB;
System.out.println(total);

Which is your preferred version?

Post Lecture

Synthesize today's class session

 

 

What is unclear?