Difference between revisions of "Race Conditions Studio"

From CSE231 Wiki
Jump to navigation Jump to search
(Created page with "=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...")
 
Line 10: Line 10:
  
 
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.
 
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.
 
Hint: try using the non-concurrent equivalent and see what happens.
  

Revision as of 19:23, 16 August 2017

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.

Where to Start

Navigate to the data races directory. The WordScoreDataRace.java class is the only one you will need to alter, but the WordScoreUtils.java 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 WordScoreUtils.toCleandWords() 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 WordScoreUtils.calculateScore() 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 EntryUtils.createEntryArray() 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 EntryUtils.createEntry() method. You can convert the array of entries into a map using the EntryUtils.createTreeMapFromArrayOfEntries() method.