Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Computer Science: After the question you will see the project written for homewo

ID: 3784533 • Letter: C

Question

Computer Science:

After the question you will see the project written for homework last week that is a part of this:

ComputationThread.java

public class ComputationThread extends Thread {

   int a = 3;
   int b = 6;

   @Override
   // define the body which need to be executed by the thread
   public void run() {
       // TODO Auto-generated method stub
       for (int i = 0; i < 1000; i++)
           System.out.println(getName()
                   + " currently running and executing add operation on a and b and the result is " + (a + b));
   }
}

ControllerThread.java

/**
*
* @author Controls all the threads
*
*/
public class ControllerThread {

   // number of objects of ThreadIo and computationThread class to be created
   static final int NUMBER_OF_THREADS = 5;

   public static void main(String[] args) throws InterruptedException {
       // create an array of 5 objects of each ThreadIo and computationThread
       // class
       ThreadIO IOobjects[] = new ThreadIO[NUMBER_OF_THREADS];
       ComputationThread computationThread[] = new ComputationThread[NUMBER_OF_THREADS];

       // store the time of the IO objects of each thread and the
       // computationThread
       long IOobjectsTime[] = new long[NUMBER_OF_THREADS];
       long computationThreadTime[] = new long[NUMBER_OF_THREADS];

       long scheduleStartTime = 0;
       long scheduleEndTime = 0;
       for (int i = 0; i < NUMBER_OF_THREADS; i++) {
           // initialising the objects
           IOobjects[i] = new ThreadIO();
           IOobjects[i].setName("IO Thread " + (i + 1));
           computationThread[i] = new ComputationThread();
           computationThread[i].setName("Computation Thread " + (i + 1));

       }
       // record the start schedule Time
       scheduleStartTime = System.currentTimeMillis();
       for (int j = 0; j < NUMBER_OF_THREADS; j++) {
           // record start time of thread
           Long threadStartTime = System.currentTimeMillis();
           IOobjects[j].start();
           // wait for the thread termination
           IOobjects[j].join();
           // record end time of thread
           Long threadEndTime = System.currentTimeMillis();
           // store the execution time of IO thread
           IOobjectsTime[j] = (threadEndTime - threadStartTime);
       }

       for (int j = 0; j < NUMBER_OF_THREADS; j++) {
           // record start time of thread
           Long threadStartTime = System.currentTimeMillis();
           computationThread[j].start();
           // wait for the thread termination
           computationThread[j].join();
           // record end time of thread
           Long threadEndTime = System.currentTimeMillis();
           // store the execution time of computation thread
           computationThreadTime[j] = (threadEndTime - threadStartTime);
       }

       // record the end time of the schedule
       scheduleEndTime = System.currentTimeMillis();

       System.out.println();
       System.out.println();
       System.out.println("******************************************************");
       System.out.println("Computation time of IO Threads");
       System.out.println("******************************************************");
       for (int j = 0; j < NUMBER_OF_THREADS; j++) {
           System.out.println("Time taken by " + IOobjects[j].getName() + " to execute :" + IOobjectsTime[j]
                   + " milliseconds");
       }
      
       System.out.println();
       System.out.println();
       System.out.println("******************************************************");
       System.out.println("Computation time of Computational Threads");
       System.out.println("******************************************************");
       for (int j = 0; j < NUMBER_OF_THREADS; j++) {
           System.out.println("Time taken by " + computationThread[j].getName() + " to execute :" + computationThreadTime[j]
                   + " milliseconds");
       }
      
       System.out.println();
       System.out.println();
       System.out.println("******************************************************");
       System.out.println("Computation time of Schedule all the Threads");
       System.out.println("******************************************************");
       System.out.println("Time Taken to schedule and run all the threads: "+(scheduleEndTime -scheduleStartTime)+ " milliseconds");
   }

}

ThreadIO.java

/**
*
* @author This thread perform the IO operation
*
*/
public class ThreadIO extends Thread {

   @Override
   // define the body which need to be executed by the thread
   public void run() {
       // TODO Auto-generated method stub
       for (int i = 0; i < 1000; i++)
           System.out.println(getName() + " currently running");
   }

}

The link provides you a link to the answer for homework 1 which is above: https://www.chegg.com/homework-help/questions-and-answers/assignment-demonstrates-understanding-create-use-threads-also-serves-get-started-final-pro-q17904944

Thank you!

This assignment demonstrates your understanding of the various issues around the OS scheduler. It also serves to help move you along with the final project Before attempting this project, be sure you have completed all of the reading assignments, hands-on labs, discussions, and assignments to date. 1. Take the project you wrote in the homework last week and do a FCFS scheduler with 5 IO bound threads (1 class and 5 processor bound threads (another class) a) The IO bound class will be a thread class that runs an IO intensive operation. You can write to the system out a number of times (ie 1000) or do something like read and write a file b) The processor bound class will be a thread class that runs a computationally intensive operation. You can perform some math computation a number of times. No IO in the loop. c) Create a controller class that implements an FCFS schedule and instantiates 5 objects of each class and runs each object 2) Take the start and stop time for each thread and print out the time it takes to run. 3) Take the start and stop time to schedule and run all the threads and print out the time to run. 4) Run the program 3 times a) First time intersperse the IO bound and computationally intensive operations (call the start method) b) Second time run the lo bound threads first and the processor bound second. c) Third time run the processor bound threads first and the IO bound second. 5) Attach your code as well as a document. The document should include snapshots (enough to demonstrate it ran) of the running code as well as results in spreadsheet form. Results should also list the wait time for each thread. Also calculate for each of the 3 scenarios the average wait time for the O bound threads, processor bound threads and the overall run time for all of the threads Include a lessons learned contrasting how the Java environment handled the 3 scenarios 6) Code should follow standard practices with good commenting, variable and method names, and good formatting..... its a 400 level course.

Explanation / Answer

1.Wait time is

      Time to get CPU (Time between , time when Thread.start is called and

                                                time when "run" method is started (means thread get CPU)

       Time for IO wait

                  Only applicable for IO thread

                  Time waited for System.out.printl to complete (not accurate)

2.We can't calculate exact wait time

3. I created following variable in each type of Thread class

   long startTime,stopTime,runningTime , waitTime , startExecutionTime;
         startTime When called Thread.Start
        startExecutionTime when run method started execution (get the CPU)
        stopTime is when run() method completed
        waitTime is Time Difference between startTime,startExecutionTime + wait for IO

4. Please check comments in Code

5. startIOThreads ,startComputationThreads methods are used to start certain type of threads

    We can re arrange calling these methods to start required threds first

    for example if we want start IOThreads first the

startIOThreads()// First statement

startComputationThreads() //next

     you can comment the requied method if you don't want to start any kind of threads

   comment calling of startIOThreads()if you don't want to start IO threads

Code

ThreadIO.java :-

-------------------------         

/**
*
* @author This thread perform the IO operation
*
*/
public class ThreadIO extends Thread {
   //Variable for Start and Stop time...
       /**
        * startTime When called Thread.Start
        * startExecutionTime when run method started execution (get the CPU)
        * stopTime is when run() method completed
        * waitTime is Time Difference between startTime,startExecutionTime + wait for IO
        */
       long startTime,stopTime,runningTime , waitTime , startExecutionTime;
       @Override
       public synchronized void start() {
          // set Start time to current System time
              startTime = System.currentTimeMillis();
          // TODO Auto-generated method stub
          super.start();
       }
   @Override
   // define the body which need to be executed by the thread
   public void run() {
       // set Start Execution time to current System time
       startExecutionTime = System.currentTimeMillis();
     
       for (int i = 0; i < 1000; i++){
           //Calculate approximate wait start time for IO , Exact time can't be calculated
           long longStartIowait = System.currentTimeMillis();
         
           System.out.println(getName() + " currently running");
           //Calculate approximate wait stop time for IO , Exact time can't be calculated
           long longStopIowait = System.currentTimeMillis();
           //Add to Wait Time
           waitTime += (longStopIowait - longStartIowait);
       }    
     
       // set Stop time to current System time
       stopTime = System.currentTimeMillis();
       //Calculate wait time
       waitTime += startExecutionTime - startTime;
       //Calculate Running time
       runningTime = stopTime - startTime;
   //Print Wait Time
       //System.out.println("Wait Time of Thread "+getName() + "in MilliSecs is " + waitTime);
       //Print Running Time
       //System.out.println("Running Time of Thread "+getName() + "in MilliSecs is " + runningTime);
   }

}

ComputationThread.java

------------------------------------------

public class ComputationThread extends Thread {

   int a = 3;
   int b = 6;
   //Variable for Start and Stop time...
   /**
    * startTime When called Thread.Start
    * startExecutionTime when run method started execution (get the CPU)
    * stopTime is when run() method completed
    * waitTime is Time Difference between startTime,startExecutionTime
    */
   long startTime,stopTime,runningTime , waitTime , startExecutionTime;

   @Override
public synchronized void start() {
   // set Start time to current System time
       startTime = System.currentTimeMillis();
   // TODO Auto-generated method stub
   super.start();
}

@Override
   // define the body which need to be executed by the thread
   public void run() {
       // set Start Execution time to current System time
       startExecutionTime = System.currentTimeMillis();
       //Computation Thread should not do any I/O like System.out.println so remove
       for (int i = 0; i < 1000; i++){
           //We do some Square root here to consume CPU
           Math.sqrt(i);
       }
       // set Stop time to current System time
       stopTime = System.currentTimeMillis();
       //Calculate wait time
       waitTime = startExecutionTime - startTime;
       //Calculate Running time
       runningTime = stopTime - startTime;
       //Print Wait Time
       //System.out.println("Wait Time of Thread "+getName() + "in MilliSecs is " + waitTime);
       //Print Running Time
       //System.out.println("Running Time of Thread "+getName() + "in MilliSecs is " + runningTime);
    }
}

ControllerThread.java

------------------------------------------

/**
*
* @author Controls all the threads
*
*/
public class ControllerThread {

   // number of objects of ThreadIo and computationThread class to be created
   static final int NUMBER_OF_THREADS = 5;

    // create an array of 5 objects of each ThreadIo and computationThread
   // class
   ThreadIO IOobjects[] = new ThreadIO[NUMBER_OF_THREADS];
   ComputationThread computationThread[] = new ComputationThread[NUMBER_OF_THREADS];
   //Method to initialize the threads
   public void initilaizeThread(){
       for (int i = 0; i < NUMBER_OF_THREADS; i++) {
           // initializing the objects
           IOobjects[i] = new ThreadIO();
           IOobjects[i].setName("IO Thread " + (i + 1));
           computationThread[i] = new ComputationThread();
           computationThread[i].setName("Computation Thread " + (i + 1));

       }
   }
   //Method to start all IO threads
   public void startIOThreads(){
       for (int j = 0; j < NUMBER_OF_THREADS; j++) {         
           IOobjects[j].start();
           // wait for the thread termination
           try {
           IOobjects[j].join();
       } catch (InterruptedException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }        
         
       }
   }
   //Method to start all Computation Threads
   public void startComputationThreads(){
       for (int j = 0; j < NUMBER_OF_THREADS; j++) {
         
           computationThread[j].start();
           // wait for the thread termination
           try {
           computationThread[j].join();
       } catch (InterruptedException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
         
       }
   }
   //Method to Print Results
   public void printResults(){
       long totalExecutionTime = 0;
       long totalWaitTime = 0;
       System.out.println();
       System.out.println();
       System.out.println("******************************************************");
       System.out.println("Computation time of IO Threads");
       System.out.println("******************************************************");
       for (int j = 0; j < NUMBER_OF_THREADS; j++) {
           if(IOobjects[j].startTime == 0){
               continue;
           }
           totalExecutionTime += IOobjects[j].runningTime;
           totalWaitTime += IOobjects[j].waitTime;
           System.out.println("Wait Time of " + IOobjects[j].getName() + " :" + IOobjects[j].waitTime
                   + " milliseconds");
           System.out.println("Time taken by " + IOobjects[j].getName() + " to execute :" + IOobjects[j].runningTime
                   + " milliseconds");
       }
    
       System.out.println();
       System.out.println();
       System.out.println("******************************************************");
       System.out.println("Computation time of Computational Threads");
       System.out.println("******************************************************");
       for (int j = 0; j < NUMBER_OF_THREADS; j++) {
           if(computationThread[j].startTime == 0){
               continue;
           }
           totalExecutionTime += computationThread[j].runningTime;
           totalWaitTime += computationThread[j].waitTime;
           System.out.println("Wait Time of " + computationThread[j].getName() + " :" + computationThread[j].waitTime
                   + " milliseconds");
           System.out.println("Time taken by " + computationThread[j].getName() + " to execute :" + computationThread[j].runningTime
                   + " milliseconds");
       }
    
       System.out.println();
       System.out.println();
     
       System.out.println("******************************************************");
       System.out.println("Total Execution Time is : "+totalExecutionTime);
       System.out.println("******************************************************");
     
       System.out.println();
       System.out.println();
     
       System.out.println("******************************************************");
       System.out.println("Total Wait Time is : "+totalWaitTime);
       System.out.println("******************************************************");
     
       System.out.println();
       System.out.println();
     
       System.out.println("******************************************************");
       System.out.println("Average Wait Time is : "+(totalWaitTime/NUMBER_OF_THREADS));
       System.out.println("******************************************************");
     
       System.out.println();
       System.out.println();
     
   }

   public static void main(String[] args) throws InterruptedException {
     

     

       long scheduleStartTime = 0;
       long scheduleEndTime = 0;
       ControllerThread controller = new ControllerThread();
       controller.initilaizeThread();
       // record the start schedule Time
       scheduleStartTime = System.currentTimeMillis();
     
       //Re Arrange of Execution of IO/Computation threads
       //If you don't want to start certain type of Thread then comment
     
       controller.startComputationThreads();
       controller.startIOThreads();
     
     

       // record the end time of the schedule
       scheduleEndTime = System.currentTimeMillis();
       //Print Results
       controller.printResults();
     
       System.out.println("******************************************************");
       System.out.println("Computation time of Schedule all the Threads");
       System.out.println("******************************************************");
       System.out.println("Time Taken to schedule and run all the threads: "+(scheduleEndTime -scheduleStartTime)+ " milliseconds");
   }

}

      
      

      

                                                

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote