Transcript Exercise 3

Algorithm Programming 1
89-210
Exercise 3
Bar-Ilan University
2007-2008 ‫תשס"ח‬
by Moshe Fresko
Exercise 3 – Thread Pool
Implementation And Usage

Motivation:

Re-use of existing threads


Control the number of threads running concurrently

1.
Because thread creation is an expensive task
Not going into starvation
Write an interface Job in file Job.java that will define a new job that can
be executed in the Thread Pool.
Remember the “Command” design pattern.
interface Job {
void execute() ;
// Executes the required process
void stop() ;
// Stops the process in the middle
String getDescription();
// A description about the type of the job
}
Exercise 3 – Thread Pool
Implementation And Usage
2.
Write a general purpose class ThreadPool in file ThreadPool.java that will have the following
functionality.
class ThreadPool
{
// Constructor will get the maximum number of threads
// that can run concurrently
public ThreadPool(int numOfMaxThreads) { … }
// Method for adding a new Job for execution. It returns immediately.
public void add(Job j) { … }
// Method for getting information on the Thread Pool’s State
public ThreadPoolState getState() { … }
// Method for getting statistics for that Thread Pool
public ThreadPoolStatistics getStatistics() { … }
// Method for stopping the whole process.
void stop() { … }
}
Exercise 3 – Thread Pool
ThreadPoolState and ThreadPoolStatistics

Information on ThreadPool will be get in structures that will be defined by these definitions.
interface ThreadPoolState {
int getNumberOfThreads() ;
String[] getDescriptionsOfRunningJobs() ;
String[] getDescriptionsOfWaitingJobs() ;
}
interface ThreadPoolStatistics {
int getNumberOfExecutedJobs() ;
// The jobs that their execution is finished
int getNumberOfJobsLeftTheQueue() ; // This must be >= the number of executed jobs
int getAverageExecutionTime() ; // In milliseconds. For the executed jobs.
int getAverageWaitingTime() ; // In milliseconds. For the jobs that left the queue.
}

These structures will be filled and returned by the functions getState() and getStatistics()
functions. The returned value will be static (will not change). A new value will be returned
for each call to those functions.
Exercise 3 – Thread Pool
Implementation And Usage
Write an application Ex3 with a main function, that
will do the following.
3.
•
•
It will prompt for a user input, will run the given
command and will return again to prompt.
The following are the available commands.





HELP : Will display a general help how to use the program.
COMPRESS
FileName CompressedFileName
DECOMPRESS CompressedFileName FileName
STATE : Will print the list of running/waiting jobs.
STATISTICS : Will print the information from getStatistics()
EXIT : Stops the ThreadPool and exits from the program.
Exercise 3 – Thread Pool
Implementation And Usage
4.
5.
There will be a single Thread Pool object in the program.
Command description:

COMPRESS Algorithm FileName CompressedFileName
will create 1 job and put that into the Thread Pool.
The job will compress the given file to create the given CompressedFile.
For example:
COMPRESS A.txt CompressedA.bin
will run one job in the thread pool so that the File A.txt will be compressed
according to LZW algorithm.

DECOMPRESS CompressedFile OutputFileName
This fill open the file and will create the original file

For Example:
DECOMPRESS CompressedA.bin B.txt
will run one job in the thread pool. After the job was run and finished, B.txt must
be created with the same content as previous A.txt.
Exercise 3 – Thread Pool
Implementation And Usage

Some points:
1.
Do not forget to synchronize where necessary. Do not
over-synchronize in unnecessary places.
2.
ThreadPool class is a general utility class, and it will not
have any code connected to Compression, LZW or other
special things. You can define specific things in Ex3.java
or in other files.
3.
The getDescription() for our specific jobs might be the
following:
1.
2.
“Job for Compressing ‘File1’ into ‘File2’ with LZW algorithm.”
“Job for DeCompressing ‘File1’ into ‘File2’ with LZW algorithm.”
Exercise 3
Thread-Pool

Important:

You submit via submitex.









Course number: 89-210
Exercise: ex3
Deliver all the necessary files.
It must compile/work under Java 1.4/1.5/1.6. You can try it on the
Unix environment (on Sunshine).
Do not write debugging information to the console or any other place.
Write enough comments. Write wherever you think there is a need for
the understanding of the code.
Write your code according to the OOP principles.
Everybody must do it alone.
Deadline:

15 Jan 2008