Skip to content

Yasmin0610/OOP_Assignment_2

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 

Repository files navigation

OOP_Assignment_2

3rd assignment of the course Object Oriented Programming.

Computer Sience B.Sc. Ariel University.

Ariel University Logo

Authors

Brief Packages Description

the packeges described below are in the src folder

  • Ex2_1:

    expresses the time saving of using a Thread and a ThreadPool.

  • Ex2_2:

    extends the defult ThreadPoolExecutor so it will considerd a priority for tasks in his Queue.

Ex2_1

Files included:

  • Ex2_1.java:

    Where all the code is written.

  • Ex2_1_Test.java:

    Test class to check code correctness.

  • main.java:

    Class to see the Runtime differences using Thread and ThreadPool.

  • Ex2_1_UML:

    Picture of the Class Diagram of this package.

Ex2_1.java

This class have the following methods:

  • createTextFiles(int n, int seed, int bound)

    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.

  • getNumOfLines(String[] fileNames)

    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).

  • getNumOfLinesThreads(String[] fileNames)

    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.

  • getNumOfLinesThreadPool(String[] fileNames)

    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.

  • deleteTextFiles(int n)

    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 in createTextFiles(int n, int seed, int bound).

Ex2_1_Test.java

This class have the following Test method:

  • main()

    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.

Ex2_1_Test

main.java

This class have the following method:

  • public static void main(String[] args)

    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.

Ex2_1_main

Ex2_1_UML

Ex2_1_UML

Ex2_2

Files included:

  • Task.java:

    The Task class represents a task that can be executed by the program.

  • TaskType.java:

    An enum to prioritize the tasks.

  • Adapter.java:

    The Adapter class is an implementation of the FutureTask class and Comparable interface.

  • CustomExecutor.java:

    CustomExecutor is a class that extends ThreadPoolExecutor and adds functionality for handling tasks with different priorities.

  • Ex2_2_Tests.java:

    Test class to check correctness of our code.

  • Ex2_2_UML.png:

    Picture of the Class Diagram of this package.

Task.java

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:

  • Task(Callable<V> callable, TaskType taskType)

    A private Constructor for our use only.
    Creates a new task with the given callable object and task type.

  • createTask(Callable<V> callable, TaskType taskType)

    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.

  • createTask(Callable<V> callable)

    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.

  • getTaskType()

    Return The task's type.

  • compareTo(Task task)

    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.

  • call()

    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.

TaskType.java

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.

Adapter.java

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:

  • Adapter(Callable<V> callable, int priority)

    Constructs a new Adapter instance, encapsulating the provided Callable and priority value.

  • compareTo(Adapter taskFuture)

    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.

  • getPriority()

    This method returns the priority value of this Adapter instance.

CustomExecutor.java

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:

  • CustomExecutor()

    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.

  • submit(src.Ex2_2.Task<V> task)

    Submits a task to the thread pool.

  • submit(Callable<V> callable, TaskType taskType)

    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.

  • submit(Callable<V> callable)

    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.

  • beforeExecute(Thread t, Runnable r)

    This method is called before a task is executed.
    This method decrements the count of the priority of the task in the QueuePriorityArr.

  • getCurrentMax()

    Returns the current maximum priority among the tasks in the queue.

  • gracefullyTerminate()

    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

Ex2_2_Tests.java

This class have the following Test methods:

  • partialTest()

    A test that was given to us with the assignment.

Tests passed

test pass

Test output

logger

Ex2_2_UML.png

Ex2_2_UML

Running

  • 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).

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%