Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS2130 Programming Language Concepts Coursework 2006 Problem 1. A Generic BoundedBuffer (25 marks) Modify the BoundedBuffer class that appeared in unit 6 of the lecture notes so that it is generic (like all the standard collection classes in Java 1.5 such as ArrayList) and so can hold values of any specified data type. Also add appropriate public accessor methods to return i. ii. iii. iv. the capacity of the buffer the number of data items currently in the buffer whether the buffer is empty or not whether the buffer is full or not Write a generic BoundedBuffer class as described above and write a short test program to test instantiations of this buffer class. Problem 2. Barrier Synchronisation (35 marks) The barrier synchronization in unit 7 of the lecture notes could be described as a counting barrier as a fixed number of threads must arrive at the barrier before all are released. With a signalling barrier ANY number of ‘worker’ threads can arrive at the barrier and should be held there (by calling the barrier’s checkIn method) until a ‘control’ thread releases them all by calling the barrier’s release method. Design an implement a signalling barrier. Be sure to avoid race conditions whereby worker threads arriving after the release call, but before all waiting threads have crossed the barrier, can sneak through. Those worker threads that arrive late should be forced wait for the next call of release by a control thread. Consider what to do if a second ‘control’ thread calls release whilst a release is actually in progress. Should this call be ignored or should it allow any worker threads that have arrived at the barrier between the two release calls to proceed? Either approach is sensible, but the two approaches will require different coding of the release method. Choose one approach and implement it. You should also submit a short multi-threaded test program suitable for testing your signalling barrier class. Problem 3. A Multi-threaded Producer Consumer Application (40 marks) In the Java code on pp3–4, a main thread sets up a file for output and then loops one hundred times getting data to process by calling a static method getData from a class DataClass. This method produces data at random intervals. The main thread then creates a slave thread to process each data item, starts the slave thread and then, after outputting a progress message to System.out, the main thread loops back to get the next piece of data. The main thread terminates after a certain number of iterations of the loop after outputting an informatory message to the file System.out. Just before terminating the main thread outputs a farewell message to System.out. A slave thread processes its data by calling the static method processData from the class DataClass. The data takes a varying amount of time to process the data and produce a result. The result is then written to a results file whose name specified on the command-line or to a file results.log. The thread then terminates after outputting an informatory message to the file System.out. CS2130-Cwk 1 5/3/2017 Unfortunately the output of the program is not synchronised and so occasionally output from two threads is jumbled. Also it is found that the overheads of creating a new thread to process each piece of data is rather wasteful of machine resources since thread creation is CPU intensive and also terminated threads consume some memory resources until the whole program terminates. It is required to modify the program to remedy these defects: firstly output to the two output streams should be properly synchronized, secondly the main thread and the slave threads should be restructured to use a fixed number of threads 10 (say) as indicated below. After setting up a file for output, the main thread should first create the requisite number of slave threads and start them executing. It should then enter a loop in which it calls the method getData and then deposits the resulting data in a suitable data structure where it can be retrieved later for processing by one of the slave threads. After being started the slave threads should each output a suitable informatory message to System.out and then enter a loop in which they try to retrieve data passed to them by the main thread for processing. If no data is immediately available the slave thread should wait for the main thread to produce some. After retrieving the data the slave should process the data by calling the method processData and write the result to file as above. After outputting an informatory message to the file System.out, the thread should then loop back to process the next piece of data. The slave threads should be such that the when the main thread terminates and all slave threads are idle, the whole program terminates. It should be possible to specify the number of slave threads and the number of iterations of the main thread on the program’s command-line along with the name of the results file. If values are not supplied on the command-line, the default values of 10, 100 and results.log should be used for the number of threads, number of iterations and name of the results file respectively. Hints: Material discussed in Units 1-7 will be needed for this problem. For problem 3 it will be no longer possible to pass the data to be processed to slave threads via their constructor. You will need to analyse the synchronisation requirements of the program with regard to output and access to shared data and then code the main thread and slave thread classes appropriately. The class DataClass may be found on the module web-site in the Coursework folder, along with listings of the classes DoIt and Slave. You must not modify the file DataClass.java in any way. This class simply simulates a data source that produces data asynchronously at varying time intervals and also simulates lengthy computations of varying complexity to calculate results. The rate of data production and the ratio of the average time of data processing to the average production time may be accessed and updated by various accessor and mutator methods of the class. CS2130-Cwk 2 5/3/2017 Submission requirements: Submit solutions for all three problems as indicated. All Java files submitted should include a comment giving your full name and UNIX user ID. Problem 1: a printer listing of the Java class BoundedBuffer and of your test program. Problem 2: a printer listing of the Java class Barrier and of your test program. Problem 3: a printer listing of your modified Slave and DoIt classes, a listing of the results file results.log produced by one run of your program plus a listing of the output produced on System.out by the same program run. The last mentioned listing can be produced by re-directing program output to a file using IO-redirection in Unix/Linux or in the Windows application cygwin. Alternatively, in most Java IDEs you can save the contents of the “terminal’ or ‘console’ window to a file. Submission of the file DataClass.java is NOT required. Submit your work to Computer Science Reception, room MB216C, by: 4.00 PM on Tuesday March 7th 2006. Work handed in after the due date will incur a penalty of 5 marks per working day (from the total of 100 marks). A Computer Science Coursework Submission Form (available from CS Reception) must be completed and attached to the front of your submission. Approximately 75% of the marks will be available for solutions which are both syntactically and logically correct. The remaining 25% of the marks will be available for the coding style and efficiency of the solutions. Thus code should be properly laid out and appropriately commented. If the files are laser-printed, a fixed-width font such as Courier 10 must be used. You are also advised to test your solutions thoroughly before submission or, at the very least, check (and correct) their syntax by using the Java compiler. You should retain on-line copies of all the files that you submit. ///////////////////////// DoIt.java //////////////////////////// import java.io.*; public class DoIt { public static int MAX_ITERATIONS = 100; //say public static void main(String[] args) throws IOException { float data; Slave s; PrintWriter resultsLog; String resultsFile = "results.log"; if (args.length >= 1) { resultsFile = args[0]; if (args.length > 1) System.err.println("Extra command line args ignored"); } CS2130-Cwk 3 5/3/2017 try{ resultsLog = new PrintWriter(new FileWriter(new File(resultsFile)), true); /* enable autoflush */ } catch (IOException e) { System.err.println("File " + resultsFile + " not found"); throw e; } for (int count = 1; count <= MAX_ITERATIONS; count++) { data = DataClass.getData(); s = new Slave("Thread " + count, data, resultsLog); s.start(); System.out.print("Slave thread " + count); System.out.println(" has been started"); } System.out.println("Main thread terminating!"); } } ///////////////////////// Slave.java //////////////////////////// import java.io.*; public class Slave extends Thread { private float data; private String ID; private PrintWriter results; public Slave(String ID, this.ID = ID; this.data = data; results = resFile; } float data, PrintWriter resFile) { public void run() { float res; res = DataClass.processData(data); results.println("Slave " +ID); results.print(" Data: " + data); results.println(" Result: " + res); System.out.print("Slave " + ID); System.out.println(" is terminating!"); } } CS2130-Cwk 4 5/3/2017