Advertisement
Guest User

Updated Test.Java

a guest
Jan 14th, 2021
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 26.10 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package test;
  7.  
  8. /*
  9.  * This file contains the main function which is called on execution and is
  10.  * used to start up all the relevant threads in order to complete the
  11.  * simulation. The main function takes 3 parameters as input through command
  12.  * line: nWorkers, nBuyers and sTime, in that order.
  13.  */
  14.  
  15. import java.util.Random;
  16. import java.util.concurrent.Semaphore;
  17. import java.util.concurrent.locks.ReentrantLock;
  18.  
  19. public class Test {
  20.    
  21.     /*
  22.      * Integer used to store the number of worker threads.
  23.      */
  24.     private static int nWorkers = 5;
  25.    
  26.     /*
  27.      * Integer used to store the number of buyer threads.
  28.      * CHANGE THIS TO 5 AND THE PROGRAM RUNS FINE!
  29.      */
  30.     private static int nBuyers = 5;
  31.    
  32.     /*
  33.      * Integer used to store the interval at which the ResourceSupplier supplies
  34.      * the warehouse with resources.
  35.      */
  36.     private static int sTime = 1000;
  37.    
  38.     /*
  39.      * An array of buyer threads used to simulate buyers
  40.      * of the warehouse.
  41.      */
  42.     private static Buyer[] buyers;
  43.    
  44.     /*
  45.      * An array of DispatchWorkers threads used to simulate workers in
  46.      * the warehouse.
  47.      */
  48.     private static DispatchWorker[] workers;
  49.    
  50.     /*
  51.      * A ResourceSupplier thread used to simulate a resource supplier for
  52.      * the warehouse.
  53.      */
  54.     private static ResourceSupplier supplier;
  55.    
  56.     /*
  57.      * A warehouse object acting as common object for all threads to communicate
  58.      * through and interract with each other.
  59.      */
  60.     private static Warehouse warehouse;
  61.    
  62.     /*
  63.      * A Random variable used to generate random values.
  64.      */
  65.     private static Random rand;
  66.    
  67.     /*
  68.      * This function is used to initiate all other threads and start them all.
  69.      */
  70.     public static void main(String[] args) {        
  71.         //A boolean used to store if a buyer is prime or not.
  72.         boolean isPrime;
  73.        
  74.         //Integer used to count the number of prime buyers.
  75.         int iPrime = 0;
  76.        
  77.         //Long used to store total waiting time of all prime buyers.
  78.         long timePrime = 0;
  79.        
  80.         //Long used to store total waiting time of all normal buyers.
  81.         long timeNormal = 0;
  82.        
  83.         //Instantiate the random variable.
  84.         rand = new Random();
  85.        
  86.         //Check if the amount of arguments given is correct.
  87.        
  88.         //The program can start it's main execution.
  89.         //Instantiate a new warehouse.
  90.         warehouse = new Warehouse();
  91.  
  92.         //Instantiate the workers and buyers array.
  93.         workers = new DispatchWorker[nWorkers];
  94.         buyers = new Buyer[nBuyers];
  95.  
  96.         //Instantiate the ResourceSupplier thread.
  97.         supplier = new ResourceSupplier(warehouse,
  98.                                         "ResourceSupplier",
  99.                                         sTime);
  100.         //Start the ResourceSupplier thread.
  101.         supplier.start();
  102.  
  103.         //Enumerate through all buyer threads. we can't use:
  104.         //for (Buyer buyer : buyers) as that would not assign to the
  105.         //array but to the temporary variable.
  106.         for (int j = 0; j < buyers.length; j++) {
  107.             //Generate a random value and, if bigger than 3, assign
  108.             //true to isPrime, false otherwise.
  109.             isPrime = (rand.nextInt(10) + 1 > 3);
  110.  
  111.             //Instantiate a new buyer thread in the jth cell of the
  112.             //array with name equal to buyer(j + 1) to counter the fact
  113.             //that arays start at 0.
  114.             buyers[j] = new Buyer(warehouse,
  115.                                   isPrime,
  116.                                   "buyer" + (j + 1));
  117.             //Start the buyer thread.
  118.             buyers[j].start();
  119.  
  120.             //Notify the user that a new buyer thread has been started.
  121.             System.out.println("started buyer: " + (j + 1));
  122.         }
  123.  
  124.         //Enumerate through all worker threads. we can't use:
  125.         //for (Worker worker : workers) as that would not assign to the
  126.         //array but to the temporary variable.
  127.         for (int j = 0; j < workers.length; j++) {
  128.             //Instantiate a new worker thread in the jth cell of the
  129.             //array with name equal to worker(j + 1) to counter the fact
  130.             //that arays start at 0.
  131.             workers[j] = new DispatchWorker(warehouse,
  132.                                         "worker" + (j + 1));
  133.  
  134.             //Start the worker thread.
  135.             workers[j].start();
  136.  
  137.             //Notify the user that the worker thread has started.
  138.             System.out.println("started worker: " + (j + 1));
  139.         }
  140.  
  141.         //All threads are running.
  142.  
  143.         try{
  144.             //Notify the user that we are waiting on the users to
  145.             //finish.
  146.             System.out.println("Joining buyers. ");
  147.  
  148.             //Enumerate through all buyers.
  149.             for (Buyer buyer : buyers) {
  150.                 //Wait untill the buyer thread is finished executing.
  151.                 buyer.join();
  152.                 //Notify the user a thread has finished execution.
  153.                 System.out.println("Joining thread: " +
  154.                                    buyer.getName());
  155.  
  156.                 //Display the avarage time for the buyer.
  157.                 System.out.println("Buyer: " + buyer.getName() +
  158.                                    " took on avarage: " +
  159.                                    buyer.avarageTime() +
  160.                                    "ms. ");
  161.  
  162.                 //Check if the buyer is prime
  163.                 if(buyer.isPrime()){
  164.                     //Add the waiting time to the total waiting time of
  165.                     //prime buyers
  166.                     timePrime += buyer.avarageTime();
  167.                     //increase the number of prime buyers.
  168.                     iPrime++;
  169.                 } else
  170.                     //add the waiting time to the total waiting time of
  171.                     //normal buyers.
  172.                     timeNormal += buyer.avarageTime();
  173.             }
  174.             //All packs have been delivered. Notify the user of this
  175.             //fact.
  176.             System.out.println("All Packs Delivered.");
  177.             //Enumerate through all workers.
  178.             for (DispatchWorker worker : workers) {
  179.                 //Notify the thread it should terminate it's main loop.
  180.                 worker.runThread(false);
  181.                 //Interrupt the thread so that it returns to evaluating
  182.                 //if it should run the main loop again.
  183.                 worker.interrupt();
  184.                 //The thread wil now finish and we can wait for it.
  185.                 worker.join();
  186.             }
  187.  
  188.             //Notify the supplier to stop it's main loop. We don't have
  189.             //to interrupt this thread as there is no manner in which it
  190.             //can get stuck and not return to the main loop evaluation
  191.             //after a small amount of time.
  192.             supplier.runThread(false);
  193.             //Wait for the supplier thread to finish.
  194.             supplier.join();
  195.  
  196.             //Notify the user of the fact all threads have finished
  197.             //execution.
  198.             System.out.println("All threads finished. ");
  199.  
  200.             //Show the avarage waiting times for prime buyers
  201.             System.out.println("The avarage waiting time "
  202.                     + "for prime buyers is"
  203.                     + timePrime / iPrime
  204.                     + "ms. ");
  205.  
  206.             //Show the avarage waiting times for normal buyers. The
  207.             //number of normal buyers is calculated by subtracting the
  208.             //number of prime buyers of the total number of buyers.
  209.             System.out.println("The avarage waiting time "
  210.                     + "for normal buyers is "
  211.                     + timeNormal / (nBuyers - iPrime)
  212.                     + "ms. ");
  213.  
  214.         //Catch any interruptExceptions which may happen.
  215.         } catch(InterruptedException e){
  216.             //Print the stackTrace to show where the InterruptException
  217.             //happend. During normal execution this should not happen.
  218.             //Therefor we want to show where the InterruptException
  219.             //Came from and what triggered this behavior.
  220.             e.printStackTrace();
  221.         }
  222.         //Notify the user that the program has finished execution.
  223.         System.out.println("Simulation finished. ");
  224.     }
  225.    
  226.     public static class Order {
  227.         /*
  228.          * An integer used to store the number of packages to order.
  229.          */
  230.         private int nPacks;
  231.  
  232.         /*
  233.          * Buyer refrence used to store the buyer who made the order.
  234.          */
  235.         private Buyer buyer;
  236.  
  237.         /*
  238.          * Boolean used to store if the order is prime or not.
  239.          */
  240.         private boolean prime;
  241.  
  242.         /*
  243.          * Long used to store the time at which the order was placed.
  244.          */
  245.         private long orderTime;
  246.  
  247.         /*
  248.          * Constructor for the order class.
  249.          */
  250.         public Order(int nPacks, Buyer buyer, boolean prime) {
  251.             this.nPacks = nPacks;
  252.             this.buyer = buyer;
  253.             this.prime = prime;
  254.         }
  255.  
  256.         /*
  257.          * Obtain the number of packages in the order.
  258.          */
  259.         public int getnPacks() {
  260.             return nPacks;
  261.         }
  262.  
  263.         /*
  264.          * Set the number of packages in the order.
  265.          */
  266.         public void setnPacks(int nPacks) {
  267.             this.nPacks = nPacks;
  268.         }
  269.  
  270.         /*
  271.          * Obtain the buyer who made the order.
  272.          */
  273.         public Buyer getBuyer() {
  274.             return buyer;
  275.         }
  276.  
  277.         /*
  278.          * Set the buyer who made the order.
  279.          */
  280.         public void setBuyer(Buyer buyer) {
  281.             this.buyer = buyer;
  282.         }
  283.  
  284.         /*
  285.          * Obtain if this order is prime.
  286.          */
  287.         public boolean isPrime() {
  288.             return prime;
  289.         }
  290.  
  291.         /*
  292.          * Set if this order is prime.
  293.          */
  294.         public void setPrime(boolean prime) {
  295.             this.prime = prime;
  296.         }
  297.  
  298.         /*
  299.          * Get the time at which the order was placed.
  300.          */
  301.         public long getOrderTime() {
  302.             return orderTime;
  303.         }
  304.  
  305.         /*
  306.          * Set the time at which the order was placed.
  307.          */
  308.         public void setOrderTime(long orderTime) {
  309.             this.orderTime = orderTime;
  310.         }
  311.     }
  312.    
  313.     public static class Buyer extends Thread{
  314.    
  315.         /*
  316.          * Integer variable used to store the amount of packages bought.
  317.          */
  318.         private int packsBought = 0;
  319.  
  320.         /*
  321.          * Boolean used to store if the buyer makes used of prime service.
  322.          */
  323.         private boolean isPrime;
  324.  
  325.         /*
  326.          * A random variable used to generate random numbers.
  327.          */
  328.         private Random rand;
  329.  
  330.         /*
  331.          * A refrence to warehouse used to communicate with the other threads.
  332.          */
  333.         private Warehouse warehouse;
  334.  
  335.         /*
  336.          * Long used to store the time the buyer has been waiting for all orders
  337.          * consecutively.
  338.          */
  339.         private long waitingTime = 0;
  340.  
  341.         /*
  342.          * The constructor of the buyer class.
  343.          */
  344.         public Buyer(Warehouse warehouse, Boolean isPrime, String name) {
  345.             super(name);
  346.             this.isPrime = isPrime;
  347.             this.rand = new Random();
  348.             this.warehouse = warehouse;
  349.         }
  350.  
  351.         /*
  352.          * The run method which is ran once the thread is started.
  353.          */
  354.         public void run() {
  355.             //Run until the thread has bought 10 packages, this ensures the thread
  356.             //will eventually stop execution automatically.
  357.             for(packsBought = 0; packsBought < 10; packsBought++)
  358.             {
  359.                 try {
  360.                     //Sleep for a random amount of time between 1 and 50
  361.                     //milliseconds.
  362.                     Thread.sleep(this.rand.nextInt(49) + 1);
  363.                     //Catch any interruptExceptions.
  364.                 } catch (InterruptedException ex) {
  365.                     //There is no problem if this exception is thrown, the thread
  366.                     //will just make an order earlier than planned. that being said
  367.                     //there should be no manner in which this exception is thrown.
  368.                 }
  369.  
  370.                 //Create a new order.
  371.                 Order order = new Order(this.rand.nextInt(3)+ 1,
  372.                                         this,
  373.                                         this.isPrime);
  374.  
  375.                 //Set the time at which the order was placed as now.
  376.                 order.setOrderTime(System.currentTimeMillis());
  377.  
  378.                 //place the newly created order in the warehouse.
  379.                 this.warehouse.placeOrder(order);
  380.             }
  381.  
  382.             //Notify the thread has finished execution.
  383.             System.out.println("Thread: " + super.getName() + " has finished.");
  384.         }
  385.  
  386.         /*
  387.          * Returns the avarage time the buyer needed to get his orders.
  388.          */
  389.         public long avarageTime()
  390.         {
  391.             return this.waitingTime / this.packsBought;
  392.         }
  393.  
  394.         /*
  395.          * Check if the thread makes use of the prime service.
  396.          */
  397.         public boolean isPrime() {
  398.             return this.isPrime;
  399.         }
  400.  
  401.         /*
  402.          * Get the total waiting time of this buyer
  403.          */
  404.         public long getWaitingTime() {
  405.             return waitingTime;
  406.         }
  407.  
  408.         /*
  409.          * Set the total waiting time of this buyer
  410.          */
  411.         public void setWaitingTime(long waitingTime) {
  412.             this.waitingTime = waitingTime;
  413.         }        
  414.     }
  415.    
  416.     public static class Warehouse {
  417.    
  418.         /*
  419.          * An array used to store the list of orders to process.
  420.          */
  421.         private Order[] buffer;
  422.  
  423.         /*
  424.          * Lock used to garantee mutial exclusion of buffer,
  425.          */
  426.         private ReentrantLock mutexBuffer;
  427.  
  428.         /*
  429.          * Semaphore used to suspend the DispatchWorkers if the warehouse if full
  430.          * of packs.
  431.          */
  432.         private Semaphore notFullBuffer;
  433.  
  434.         /*
  435.          * Semaphore use dto suspend the Buyers if the warehouse is empty.
  436.          */
  437.         private Semaphore notEmptyBuffer;
  438.  
  439.         /*
  440.          * Semaphore used to notify the workers of how many boxes are available.
  441.          */    
  442.         private Semaphore hasBoxes;
  443.  
  444.         /*
  445.          * Semaphore used to notify the workers of how much tape is available.
  446.          */    
  447.         private Semaphore hasTape;
  448.  
  449.         /*
  450.          * Define the size of the warehouse (the maximum number of packs the
  451.          * warehouse is able to store).
  452.          */
  453.         private int sizeOrderList = 100;
  454.  
  455.         /*
  456.          * Counter used to keep track of the position to insert normal orders
  457.          * into the buffer array.
  458.          */    
  459.         private int inNormal = 0;
  460.  
  461.         /*
  462.          * Counter used to keep track of the position to insert prime orders
  463.          * into the buffer array.
  464.          */    
  465.         private int inPrime = 0;
  466.  
  467.         /*
  468.          * Constructure for the Warehouse class.
  469.          */
  470.         public Warehouse() {
  471.             //Instantiate the array used to store orders based on the predifined
  472.             //buffer size.
  473.             buffer = new Order[sizeOrderList];
  474.  
  475.              //Initialize the lock
  476.             this.mutexBuffer = new ReentrantLock();
  477.  
  478.             //Initialize the semaphore used to suspend the DispatchWorkers to the
  479.             //size of the warehouse such that we assume the warehouse to be empty
  480.             //when the simulation starts. (all packs can be made)
  481.             this.notFullBuffer = new Semaphore(sizeOrderList);
  482.  
  483.             //Initialize the semaphore used to suspend the Buyers to the
  484.             //0 of the warehouse such that we assume the warehouse to be empty
  485.             //when the simulation starts. (no packs can be bought)
  486.             this.notEmptyBuffer = new Semaphore(0);
  487.  
  488.             //Instantiate the semaphore used to keep track of the number of boxes
  489.             //available in the warehouse. We assume that at the start of the
  490.             //simulation no boxes are present.
  491.             this.hasBoxes = new Semaphore(0);
  492.  
  493.             //Instantiate the semaphore used to keep track of the amount of tape
  494.             //available in the warehouse. We assume that at the start of the
  495.             //simulation no tape is present.
  496.             this.hasTape = new Semaphore(0);
  497.         }
  498.  
  499.         /*
  500.          * Given an order in input, we add the order to the buffer list such that
  501.          * all prime orders are at the start of the list and the non prime orders
  502.          * are behind that. both in FIFO order.
  503.          */
  504.         public void placeOrder(Order order) {
  505.         try{
  506.             //halt untill there are enough packs to handle an order.
  507.             this.notFullBuffer.acquire();
  508.  
  509.             //Lock to signify the start of the critical section.
  510.             this.mutexBuffer.lock();
  511.  
  512.             //Insert the order in the buffer depending on prime status.
  513.             if (order.isPrime()) {
  514.                 //prime order, insert behind all prime orders in buffer.
  515.  
  516.                 //Enumerate all non prime orders in the list.
  517.                 for (int i = inPrime; i < sizeOrderList - 1; i++) {
  518.                     //Move the non prime order back 1 position in the list.
  519.                     buffer[i + 1] = buffer[i];
  520.                 }
  521.  
  522.                 // Insert the prime order.
  523.                 buffer[inPrime++] = order;
  524.  
  525.             } else {
  526.                 //No prime order, insert behind all orders in buffer.
  527.                 buffer[inPrime + inNormal++] = order;
  528.             }
  529.             //Notify the DispatchWorkers that a new order has been placed.
  530.             this.notEmptyBuffer.release();
  531.  
  532.             //Catch any InterruptException that might occure.
  533.             } catch(InterruptedException e){
  534.                 //Even though this isn't expected behavior, there is no reason to
  535.                 //notify the user of this event or to preform any other action as
  536.                 //the thread will just return to the queue before placing another
  537.                 //error if it is still required to do so.
  538.             } finally {
  539.                 //Unlock and finalize the critical section.
  540.                 mutexBuffer.unlock();
  541.             }
  542.         }
  543.  
  544.         /*
  545.          * Handle a single order from the buffer.
  546.          */    
  547.         public void handleOrder(){
  548.             //Create a variable to store the order being handled.
  549.             Order toHandle = null;
  550.  
  551.             try{
  552.                 //wait until there is an order to handle.
  553.                 this.notEmptyBuffer.acquire();
  554.  
  555.                 //Lock to signify the start of the critical section.
  556.                 this.mutexBuffer.lock();
  557.  
  558.                 //obtain the first order to handle as the first element of the buffer
  559.                 toHandle = buffer[0];
  560.  
  561.                 //move all buffer elementst back by 1 position.
  562.                 for(int i = 1; i < sizeOrderList; i++){
  563.                     buffer[i - 1] = buffer[i];
  564.                 }
  565.                 //set the last element in the buffer to null
  566.                 buffer[sizeOrderList - 1] = null;
  567.  
  568.                 //We have obtained an order from the buffer and now we can handle it.
  569.                 if(toHandle != null) {
  570.                     int nPacks = toHandle.getnPacks();
  571.  
  572.                     //wait until the appropriate resources are available.
  573.                     this.hasBoxes.acquire(nPacks);
  574.                     this.hasTape.acquire(nPacks * 50);
  575.  
  576.                     //Now we can handle the order (Simulated by sleeping. Although
  577.                     //in real live Amazon workers also have about 5ms of time per
  578.                     //package).                
  579.                     Thread.sleep(5 * nPacks);
  580.  
  581.                     //Calculate the total time this order took.
  582.                     long time = System.currentTimeMillis() -
  583.                                 toHandle.getOrderTime();
  584.  
  585.                     //Update the total waiting time for the buyer.
  586.                     toHandle.getBuyer().setWaitingTime(time +
  587.                                     toHandle.getBuyer().getWaitingTime());  
  588.  
  589.                     //Check if the order to handle is prime or not.
  590.                     if(toHandle.isPrime()) {
  591.                         //Decrement the position of which prime orders are
  592.                         //inserted into the buffer.
  593.                         inPrime--;
  594.                     } else {
  595.                         //Decrement the position of which normal orders are
  596.                         //inserted into the buffer.
  597.                         inNormal--;
  598.                     }
  599.  
  600.                     //Print a message informing the user a new order was completed.
  601.                     System.out.println("An order has been completed for: "
  602.                                         + toHandle.getBuyer().getName());
  603.  
  604.                 }else {
  605.                     //Notify the user there was a critical error obtaining the
  606.                     //error to handle. (There shouldn't exist a case where this
  607.                     //should happen but you never know.)
  608.                     System.err.println("Something went wrong obtaining an order.");
  609.                 }
  610.  
  611.                 //Notify the buyers that a new spot has been opened in the buffer.
  612.                 this.notFullBuffer.release();
  613.  
  614.             //Catch any interrupt exceptions.
  615.             } catch(InterruptedException e){
  616.                 //This is expected behavior as it allows us to force the thread to
  617.                 //revaluate it's main running loop when notifying it to finish
  618.                 //execution.
  619.             } finally {
  620.                 //Check if the current thread is locking the buffer lock. This is
  621.                 //done as in the case of an interrupt we don't want to execute this
  622.                 //code if the thread interrupted doesn't hold the lock as that
  623.                 //would result in an exception we don't want.
  624.                 if (mutexBuffer.isHeldByCurrentThread())
  625.                     //Unlock the buffer lock.
  626.                     mutexBuffer.unlock();
  627.             }
  628.         }
  629.  
  630.         /*
  631.          * Deposit nBoxes number of boxes and nTape cm of tape in the warehouse
  632.          * to be used by the workers.
  633.          */
  634.         public void depositResources(int nBoxes, int nTape) {
  635.             //Notify the workers of the amount of materials which the
  636.             //ResourceSupplier has supplied.
  637.             this.hasBoxes.release(nBoxes);
  638.             this.hasTape.release(nTape);
  639.         }
  640.     }
  641.    
  642.     public static class ResourceSupplier  extends Thread{
  643.    
  644.         /*
  645.          * A random variable used to generate random numbers.
  646.          */
  647.         private Random rand;
  648.  
  649.         /*
  650.          * An integer variable used to store how long the thread should wait
  651.          * between proviing resources to the warehouse, measured in tmilliseconds.
  652.          */
  653.         private int sTime;
  654.  
  655.         /*
  656.          * A refrence to warehouse used to communicate with the other threads.
  657.          */
  658.         private Warehouse warehouse;
  659.  
  660.         /*
  661.          * A boolean used to indicate the thread should run another cycle or not.
  662.          */
  663.         private boolean run = true;
  664.  
  665.         /*
  666.          * The constructor of the ResourceSupplier class.
  667.          */
  668.         public ResourceSupplier(Warehouse warehouse, String name, int sTime) {
  669.             super(name);
  670.             this.warehouse = warehouse;
  671.             this.sTime = sTime;
  672.             this.rand = new Random();
  673.         }
  674.  
  675.         /*
  676.          * The run method which is ran once the thread is started.
  677.          */
  678.         public void run() {
  679.             //Run until notified not to.
  680.             while(run){
  681.                 //Deposit resources in the warehouse.
  682.                 this.warehouse.depositResources(this.rand.nextInt(100),
  683.                                                 this.rand.nextInt(5000));
  684.                 try {
  685.                     //Wait for the amount of time spesified by sTime.
  686.                     Thread.sleep(this.sTime);
  687.                 } catch (InterruptedException ex) {
  688.                     //Notify the user the thread was interrupted while sleeping.
  689.                     System.out.println("Resource Supplier Interrupted. ");
  690.                     //Make sure run is false as we don't want to run another cycle.
  691.                     run = false;
  692.                 }
  693.             }
  694.         }
  695.  
  696.         /*
  697.          * A function used to indicate a thread should run another cycle or not.
  698.          */
  699.         public void runThread(Boolean run){
  700.             this.run = run;
  701.         }
  702.     }
  703.    
  704.     public static class DispatchWorker extends Thread{
  705.    
  706.         /*
  707.          * A refrence to warehouse used to communicate with the other threads.
  708.          */
  709.         private Warehouse warehouse;
  710.  
  711.         /*
  712.          * A boolean used to indicate the thread should run another cycle or not.
  713.          */
  714.         private boolean run = true;
  715.  
  716.         /*
  717.          * Constructor for the DispatchWorker class.
  718.          */
  719.         public DispatchWorker(Warehouse warehouse, String name) {
  720.             super(name);
  721.             this.warehouse = warehouse;
  722.         }
  723.  
  724.         /*
  725.          * The run method which is ran once the thread is started.
  726.          */
  727.         public void run() {
  728.             //Run until told not to.
  729.             while(run){
  730.                 //Handle an error.
  731.                 warehouse.handleOrder();
  732.             }
  733.         }
  734.  
  735.         /*
  736.          * A function used to indicate a thread should run another cycle or not.
  737.          */
  738.         public void runThread(Boolean run){
  739.             this.run = run;
  740.         }
  741.     }
  742. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement