Advertisement
Guest User

Test.java (Thread Demonstration)

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