Description
-
Concurrent File Writing
The idea behind this assignment is to get students familiar with the basics of threads in Java. The task itself is quite simple, but the implementation does involve some consideration about how multi-threaded programs work, and forces the student to think about the role each thread should play in the task.
In this assignment, students will implement a method that reads a text le into memory, concurrently rearranges this bu er, and then writes the rearranged bu er into an new output le. Figure 1 depicts this architecture in the case that the rearrangement is performed by 4 threads. The input and output bu er are di erent bu ers.
Each piece of a le being arranged is de ned as a \chunk.” We will assume that chunks are always the same size, meaning there is never a \leftover” chunk. In particular, the le size must be a multiple of the chunk size. The size of each chunk is speci ed by the user in stdin as a command line argument. The name
1
CS511 – Concurrent Programming 2 CONCURRENT FILE WRITING
ReadFile
Input Buffer
-
Thread 1
Thread 2
Thread 3
Thread 4
Buffer
WriteFile
Figure 1: Assignment architecture
of the output le is \output.txt”. The number of chunks that make up a le can be calculated by dividing the le size by the speci ed chunk size.
The order in which a le’s chunks are rearranged is based on a rearrangement pattern, similar to pattern matching. Each chunk is named in order with respect to the alphabet. A le with 5 chunks would have chunks ’a’ to ’e’. A possible reordering of this le would be the rearrangement pattern ‘a c b e d’. For example, given the le \AAA BBB CCC DDD EEE “, a speci ed chunk size of 4, and the pattern ‘a c b e d’ the output of the program should be \AAA CCC BBB EEE DDD “. The rearrangement pattern is input via stdin.
2.1 File Structure
This assignment consists of the following les:
Interval.java. Declares the Interval class.
TextSwap.java. Declares the TextSwap class. This class holds the main method from which the assignment is executed.
2
CS511 – Concurrent Programming 2 CONCURRENT FILE WRITING
Swapper.java. Declares the Swapper class.
letters.txt. A sample text le.
2.1.1 TextSwap
The TextSwap class contains much of the logic for the assignment. It has the methods:
private static String readFile (String filename) throws Exception
This method should read from a speci ed le by placing it into a String-Builder object and then returning the toString() of that object.
private static Interval[] getIntervals (int numChunks, int chunkSize)
This method returns an array of \Intervals”. An interval is just a pair of integers that specify the start and end index of a chunk. These intervals will be delegated to the appropriate threads to ensure the reordering is proper.
private static char[] runSwapper (String content, int chunkSize, int numChunks)
This method does much of the actual logic of the class. It creates the intervals, runs the Swapper threads, and returns the reordered bu er that will be written to the new le.
private static void writeToFile (String contents, int chunkSize, int numChunks) throws Exceptio
This method writes the bu er to a new le.
public static void main (String [] args)
The main should parse two command line inputs, a chunk size and a lename. The size of the le and the number of chunks should then be calculated, and the new pattern of letters should be read from stdin. If the number of chunks is more than 26, then execution should halt with an error message \Chunk size too small”. If the le size is not a multiple of the chunk size, then execution should halt with an error message \File size must be a multiple of the chunk size”. Note that there may be other methods necessary to complete this class, but they can also be inlined into these methods.
2.1.2 Swapper
This class, which should implement the Runnable interface, will write to the bu er given its Interval. It has the elds o set, interval, content, bu er:
1 public class Swapper implements Runnable {
-
private int offset ;
-
private Interval interval ;
-
private String content ;
5 private char [] buffer ;
3
CS511 – Concurrent Programming 4 SUBMISSION INSTRUCTIONS
-
…
-
}
O set: speci es the starting index in the bu er where the content will be placed.
Interval: speci es the starting and ending index of the content in the original
le that is being swapped. Content: the entire original le in a String. Bu er: The shared char[] that the result is being written to.
public void run ()
Write the speci ed content into the bu er. Helper methods may be used to retrieve the content and insert it into the proper spot in the bu er.
2.1.3 Interval
This is exactly what you would expect from any \Pair” class. There is even a Pair class in the Java library that can be used instead.
-
Your Task
Implement Swapper.java and the methods runSwapper and getIntervals in le TextSwap.java.
-
Submission Instructions
Submit a zip le named hw1 <Surname>.zip (where <Surname> should be re-placed by your surname) through Canvas containing all the les included in the stub but where all required operations have been implemented.
4