Computer Science 4721: Project 2
Due: February 28th, 2002, Before 10:15 am
Posponed: March 5th, 2002, Before 10:15 am
Thread Synchronization
Write a program in Java that creates four threads, each thread blocks
until it is notified to continue, it will print out a message that contains
its identification and a sequence number, that thread will then notify
the next thread in the chain. Assuming that the four threads in the chain
are called "A", "B", "C", "D", then two cycles of executing all threads will
produce:
A 1
B 1
C 1
D 1
A 2
B 2
C 2
D 2
Thread A will be allow to run initially, it will be continued by a notification
from thread D. Thus, thread A starts, print its message, notifies thread B,
and then waits to be notified to continue. Thread B, prints a message,
notifies thread C, and blocks, Finally, thread D runs and prints its message,
and notifies thread A.
Only one thread is notified at a time.
The main loop in the run method of each thread should contain the following:
for( int i = 0 ; i < numCycles; i++ ) {
// block, wait to be notified
// print a message
// notify the next thread in the chain
}
In this part of the project, you should implement three synchronization
methods that implement the blocking and notification. The techniques are:
-
a gateway class that only allows one thread a time to continue
-
a set of semaphores, each thread pair in the chain shares a semaphore,
the semaphores are set up to only allow one thread to continue.
- each thread class must implement a block and a
continueNext method using the thread's wait
and notify methods to perform the synchronization.
The first technique uses one object to synchronization all the threads.
The second technique uses a set of semaphores for each thread pair.
The third technique requires each thread to implement the synchronization
with Java's built-in synchronization methods directly.
Program to sum 1000 files containing 10000 integers in parallel
The files containing the integers can be found in:
/users/cs/faculty/rod/project2_data
The sum program is given as input a directory containing
1000 files. Each file contains 10000 integers, each integer is
on a separate line. The sum program should produce the total sum
of all the integers in all the files with a set of threads.
The program should contain the following threads:
-
A master thread that reads all the filenames of the files containing
the integers. The master thread will then start a set of worker
threads. The master thread then provides a filename
(a work assignment) to any worker thread that request one of the
files. The master should keep track of the total number of work
assignments. The master is also responsible for receiving the totals
of each individual file. Receipt of each sum allows the master
to reduce the number of outstanding work items.
The master thread should print out the total sum and terminate
the program when all work items are returned.
-
A worker thread is responsible for requesting a filename from
the single master thread. The worker should then open
the file and read its contents to produce the sum of integers in the file.
The sum should then be sent back to the master.
The worker should then request a new file name from the
master. If all the filenames have been handed out the
worker thread should terminate.
The sum program should accept a command line argument that the specifies
the number of worker threads. The program should also determine
the time taken to sum all the integers.
Deliverables
You should submit the following:
- an electronic copy of your project using submit-assignment,
-
paper copies of the listing of your project. You should use a2ps to produce the program's listing.
-
tests of the synchronization program with numCyclesset
to 10 for each synchronization technique.
The test output should be submitted on paper.
-
You should run the sum program with the number of workers set to
1, 2, 4, 8, and 16. Compare and discuss the times required to perform the
calculations. The discussion should be submitted on paper.