Difference between revisions of "Race Conditions Studio"

From CSE231 Wiki
Jump to navigation Jump to search
 
(10 intermediate revisions by 2 users not shown)
Line 3: Line 3:
  
 
=Background=
 
=Background=
In this studio, you will take in a collection of strings, clean up each string, and add it into another collection of strings concurrently. You will also take in a collection of words, assign a point value to each word, and then put it into a map. Although not terribly exciting, the point of this studio is to teach you how to avoid data races when performing concurrent tasks. In order to accomplish these tasks, you will use the ConcurrentLinkedQueue, array, and ConcurrentHashMap classes.
+
In this studio, you will take in a collection of strings, clean up each string, and add it into another collection of strings concurrently.  
  
==Code To Use==
+
=The Core Questions=
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html ConcurrentHashMap]
+
Really think about these questions in each case. Knowing the answers to these will tell you where the problem is.
: [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#compute-K-java.util.function.BiFunction- compute(key, remappingFunction)]
+
*What are the tasks?
 +
*What is the data?
 +
*Is the data mutable?
 +
*If so, how is it shared?
  
 
=Code To Debug=
 
=Code To Debug=
 
==Word Score==
 
==Word Score==
{{CodeToDebug|SuspectWordScore|toCleanedWordsViaArray|racecondition.studio.wordscore}}
+
{{CodeToDebug|SuspectWordScore|toCleanedWordsViaArray|racecondition.wordscore.studio}}
 
{{Parallel|public static List<String> toCleanedWordsViaArray(Collection<String> sourceLines)}}
 
{{Parallel|public static List<String> toCleanedWordsViaArray(Collection<String> sourceLines)}}
 
==Merge Sort==
 
{{CodeToDebug|SuspectMergeSort|parallelMergeSortKernel|racecondition.studio.mergesort}}
 
{{Parallel|private static void parallelMergeSortKernel(int[] data, int lowInclusive, int highExclusive, int threshold, Combiner combiner)}}
 
 
==Word Count==
 
{{CodeToDebug|SuspectWordCountTest|countWords|racecondition.studio.wordcount}}
 
{{Parallel|public static Map<String, Integer> countWords(Iterable<String> words)}}
 
  
 
=Testing Your Solution=
 
=Testing Your Solution=
 
==Correctness==
 
==Correctness==
{{TestSuite|RaceConditionTestSuite|racecondition.studio}}
+
{{TestSuite|WordScoreTestSuite|acecondition.wordscore.studio}}
 
 
{{Warning|Run Tests Repeatedly To Better Determine That You Are Not Just Getting A Lucky Run.}}
 
 
 
{{TestSuite|RepeatRaceConditionTestSuite|racecondition.studio.bettersafethansorry}}
 
 
 
<!--
 
=Where to Start=
 
 
 
Navigate to the '''data races''' directory. The <code>WordScoreDataRace.java</code> class is the only one you will need to alter, but the <code>WordScoreUtils.java</code> class contains useful methods that will help with the studio.
 
 
 
==toCleanedWords==
 
 
 
This method should take in a collection of strings, clean it, then add the strings into another collection of strings. The <code>WordScoreUtils.toCleandWords()</code> method should come in handy for cleaning up the string. All of this should be done in parallel and you should use a ConcurrentLinkedQueue to accomplish this task.
 
 
 
Hint: try using the non-concurrent equivalent and see what happens.
 
 
 
==toCleanedWordsViaArray==
 
 
 
This method should do the same thing as the toCleandWords method, but it should use arrays to avoid a data race.
 
Hint: use the Arrays.asList() method to turn your array result into a list so that you can return the data structure the method desires.
 
 
 
==createLetterToScoreMap==
 
 
 
This method should take in a collection of strings, assign a point value to the string, then put it into a map. The map should be composed of the words and their associated point values. The <code>WordScoreUtils.calculateScore()</code> method should come in handy for assigning the correct point value for each word. If the map already contains a given word (there is a duplicate word), nothing should be done to the map.
 
 
 
==createLetterToScoreMapViaArray==
 
  
This method should do the same thing as the createLetterToScoreMap method, but it should use an array of entries to avoid a data race. To do this, the <code>EntryUtils.createEntryArray()</code> method should come in handy. For each string within the collection, get the point value as you did before and create a new entry for the array using the <code>EntryUtils.createEntry()</code> method. You can convert the array of entries into a map using the <code>EntryUtils.createTreeMapFromArrayOfEntries()</code> method.
+
=Pledge, Acknowledgments, Citations=
-->
+
{{Pledge|studio-race-condition-word-score}}

Latest revision as of 16:36, 23 January 2020

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 take in a collection of strings, clean up each string, and add it into another collection of strings concurrently.

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: SuspectWordScore.java Debugging icon.png
methods: toCleanedWordsViaArray
package: racecondition.wordscore.studio
source folder: student/src/main/java

method: public static List<String> toCleanedWordsViaArray(Collection<String> sourceLines) Parallel.svg (parallel implementation required)

Testing Your Solution

Correctness

class: WordScoreTestSuite.java Junit.png
package: acecondition.wordscore.studio
source folder: testing/src/test/java

Pledge, Acknowledgments, Citations

file: studio-race-condition-word-score-pledge-acknowledgments-citations.txt

More info about the Honor Pledge