Race Condition Translation Assignment
Contents
Motivation
Race conditions can lead to difficult bugs to find and fix. We gain experience finding them and fixing them.
Background
In this studio, you will translate an array of texts in parallel.
The Core Questions
Really think about these questions in each case. Knowing the answers to these will tell you where the problem is.
- What are the tasks?
- What is the data?
- Is the data mutable?
- If so, how is it shared?
Code To Debug
Word Score
| class: | TranslationUtils.java | |
| methods: | translate | |
| package: | racecondition.translate.exercise | |
| source folder: | student/src/main/java |
method: String[] translate(String[] words, TaskFunction<String, String> translator)
(parallel implementation required)
Future<Void>[] futures = new Future[words.length];
String[] translatedWords = new String[words.length];
int[] array = { 0 };
for (array[0] = 0; array[0] < words.length; ++array[0]) {
futures[array[0]] = void_fork(() -> {
translatedWords[array[0]] = translator.apply(words[array[0]]);
});
}
join(futures);
return translatedWords;
Client
| class: | TranslationClient.java | DEMO: |
| methods: | main | |
| package: | racecondition.translate.client | |
| source folder: | src//java |
The client invokes your method with a Pig Latin tranlator:
String[] translatedWords = TranslationUtils.translate(words, new PigLatinTranslator());
Testing Your Solution
Correctness
| class: | _TranslateTestSuite.java | |
| package: | racecondition.translate.exercise | |
| source folder: | testing/src/test/java |
Pledge, Acknowledgments, Citations
| file: | studio-race-condition-translate-pledge-acknowledgments-citations.txt |
More info about the Honor Pledge