the packeges described below are in the src
folder
-
expresses the time saving of using a Thread and a ThreadPool.
-
extends the defult ThreadPoolExecutor so it will considerd a priority for tasks in his Queue.
Files included:
-
Where all the code is written.
-
Test class to check code correctness.
-
Class to see the Runtime differences using Thread and ThreadPool.
-
Picture of the Class Diagram of this package.
This class have the following methods:
-
creating n text files where each file have maximum of bound lines.
each run is different according to the seed.
This method returns a String[n] of the file names. -
This method returns the number of lines of all the files int the String[n] combine,
where n is the number of files.
This is a generic solution (each file at a time). -
This method returns the number of lines of all the files int the String[n] combine,
where n is the number of files.
This solution works with one Thread besides the Main Thread. -
This method returns the number of lines of all the files int the String[n] combine,
where n is the number of files.
This solution works with a ThreadPool. -
This method delete the n files we created with
createTextFiles(int n, int seed, int bound)
.
NOTE!
int n needs to be provided manualy, according to n value increateTextFiles(int n, int seed, int bound)
.
This class have the following Test method:
-
This Test method creats n files using
createTextFiles(int n, int seed, int bound)
and saves the names of the files it created in a String[n] array called fileNames.
Then the Test method checks if the return value of
getNumOfLines(fileNames)
,
getNumOfLinesThreads(fileNames)
,
getNumOfLinesThreadPool(fileNames)
is the same.
This class have the following method:
-
We have 8 variables:
- long start - starting the time counting.
- long end - ending the time counting.
- int filesAmount - value of n in
createTextFiles(int n, int seed, int bound)
. - int seed - value of seed in
createTextFiles(int n, int seed, int bound)
. - int bound - value of bound in
createTextFiles(int n, int seed, int bound)
. - int noThreads - the time it took to count the lines of the files generically.
- int oneThread - the time it took to count the lines of the files with one Thread.
- int ThreadPool - the time it took to count the lines of the files with a ThreadPool.
first we creats filesAmount files using
createTextFiles(int n, int seed, int bound)
and saves the names of the files it created in a String[n] fileNames.
Then we save the times it took to generate an answer with each method.
noThreads =getNumOfLines(fileNames)
,
oneThread =getNumOfLinesThreads(fileNames)
,
ThreadPool =getNumOfLinesThreadPool(fileNames)
.
and compares the RunTime differences.
output:
first of all we prints the amount of files we created, then we print the time it took for each method
and at the end, we print the amount of lines that each method counted.
Files included:
-
The Task class represents a task that can be executed by the program.
-
An enum to prioritize the tasks.
-
The Adapter class is an implementation of the FutureTask class and Comparable interface.
-
CustomExecutor is a class that extends ThreadPoolExecutor and adds functionality for handling tasks with different priorities.
-
Test class to check correctness of our code.
-
Picture of the Class Diagram of this package.
The Task class represents a task that can be executed by the program. It is implemented as a Callable object
and can be used in the Executor framework or any other framework that uses Callable objects.
The Task class also implements the Comparable interface, which allows tasks to be compared and sorted by their priority.
This class have 3 private final variables:
- private final TaskType taskType - The task's type, represented by an enumeration.
- private final Callable callable - The callable object that the task wraps.
- private final int priority - The task's priority, determined by its task type.
This class have the following methods:
-
A private Constructor for our use only.
Creates a new task with the given callable object and task type. -
Creates a new task with the given callable object and task type.
This method returns a new task with the given callable object and task type. -
Creates a new task with the given callable object and default task type.
This method returns a new task with the given callable object and task type. -
Return The task's type.
-
Compares this task to another task based on their priority.
This method returns a negative integer, zero, or a positive integer
as this task's priority is less than, equal to, or greater than the specified task's priority. -
Executes the task's callable object and returns the result.
This method returns The result of the callable's execution,
and throws any exception that may be thrown by the callable's execution.
An attached enum we received with the assignment.
This enum is given to any task and it represent the priority of the task
with Integers 1-3 where 1 is the highest priority.
The Adapter class is an implementation of the FutureTask class and Comparable interface.
It is used to encapsulate a Callable and a priority value,
which can be used to compare and order instances of this class.
This class have 1 private final variable:
- private final int priority - The task's priority, determined by its task type.
This class have the following methods:
-
Constructs a new Adapter instance, encapsulating the provided Callable and priority value.
-
Compares this Adapter instance with the specified Adapter instance which is given.
This method return a negative integer, zero, or a positive integer
as this object's priority is less than, equal to, or greater than the specified object's priority. -
This method returns the priority value of this Adapter instance.
CustomExecutor is a class that extends ThreadPoolExecutor and adds functionality for handling tasks with different priorities.
It uses a PriorityBlockingQueue to store the tasks and the priority of each task is determined by its TaskType.
The maximum pool size of the thread pool is set to the number of available processors minus one, and the core pool size
is set to half the number of available processors.
In addition to that, this class obtains an array that holds the number of tasks of each priority.
This class have 4 private final variables:
- private final int[] QueuePriorityArr - counts the tasks in the queue with consideration of priority.
- private static final int CorePoolSize - the available Processors divided by 2.
- private static final int MaximumPoolSize - the available Processors minus 1.
- private static final long keepAliveTime - the maximum time that excess idle threads will wait for new tasks before terminating in milliseconds.
This class have the following methods:
-
Constructs a CustomExecutor with the default core pool size, maximum pool size, and keep alive time.
The thread pool uses a PriorityBlockingQueue to hold the tasks. -
Submits a task to the thread pool.
-
Submits a callable task to the thread pool with a specified TaskType.
The Future's get method will return the result of callable upon successful completion. -
Submits a callable task to the thread pool. The task will be assigned the default TaskType.
The Future's get method will return the result of callable upon successful completion. -
This method is called before a task is executed.
This method decrements the count of the priority of the task in the QueuePriorityArr. -
Returns the current maximum priority among the tasks in the queue.
-
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.
The method waits for the termination of all running tasks and for the tasks in the queue to be completed.
The method will wait for a maximum of 100 milliseconds for the termination of the tasks
This class have the following Test methods:
- A test that was given to us with the assignment.
Tests passed
Test output
-
To run the repository, you will need to have JUnit 5 and a Java development environment set up.
-
Clone this repository to your chosen location using the following command:
/path/of/your/chosen/location> git clone https://github.com/avivTurgeman/OOP_Assignment_2.git
-
Open the Ex2_1_Test.java/main.java/Ex2_2_Tests.java file in your preferred
Java development environment (e.g. Eclipse, IntelliJ IDEA, etc.). -
Use your development environment's built-in support for running JUnit tests
to run the test() method in the Tests class. This is usually done by right-clicking on the test method
and selecting "Run As" > "JUnit Test" (in Eclipse)
or by using a keyboard shortcut (e.g. Ctrl+Shift+F10 in IntelliJ IDEA).