Chapter 13 |
Tasks (to be submitted by 4.05)
Task 1
Create a class called IDRegister
. The class should contain List<String>
which holds personal identification numbers and the method register
which adds a personal identification number to the list if it is not there yet. In addition, the program has to implement index
method that returns the index of a personal identification number in the list or -1 if the personal identification number is not registered. Use synchronization so that the registry list is protected against the race. Use the list as a monitor.
Create the client class to demonstrate the work of threads.
Task 2
Create several text files with a large number of numbers separated by spaces. Write a program that reads the numbers from the file and finds the sum of the numbers in each file. In the main class, create a queue into which you can add the calculated sums, and a queue in which the names of the files waiting to be processed are stored. In the main method, the names of all the files should be placed into the queue for the filenames and three threads should start their work. Each thread should take a file from the queue with the filenames, read the numbers in, find the sum of the numbers and put the sum to the queue with the results. Once the file has been processed, the thread has to attempt to take the next file from the queue and process it, etc. The main method should take and display as many numbers from the result queue as there are files.
Chapter 13 |