Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.50 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.util.Random;
  6. import java.util.Scanner;
  7. import java.util.concurrent.locks.ReentrantLock;
  8.  
  9. import static java.lang.Thread.sleep;
  10.  
  11. //Creating object for routing station
  12. class Station{
  13.     //Declaring id and workload variable for routing station
  14.     private int id;
  15.     private int workload;
  16.     //Declaring a variable for keeping track of aquired conveyor
  17.     private int aquiredConveyor;
  18.  
  19.     //Creating a constructor
  20.     public Station(int id, int workload)
  21.     {
  22.         this.id = id;
  23.         this.workload = workload;
  24.         this.aquiredConveyor = 0;
  25.     }
  26.  
  27.     //Function to set incoming conveyor
  28.     public void inConnectionSet(int conveyor)
  29.     {
  30.         System.out.printf("Station %d: In-Connection set to conveyor %d.\n", this.id, conveyor);
  31.     }
  32.  
  33.     //Function to set outgoing conveyor
  34.     public void outConnectionSet(int conveyor)
  35.     {
  36.         System.out.printf("Station %d: Out-Connection set to conveyor %d.\n", this.id, conveyor);
  37.     }
  38.  
  39.     //Function to set workload
  40.     public void workloadSet()
  41.     {
  42.         System.out.printf("Station %d: Workload set. Station %d has %d package groups to move.\n", this.id, this.id, this.workload);
  43.     }
  44.  
  45.     //Function to grant access to conveyor
  46.     public void grantAccess(int conveyor)
  47.     {
  48.         System.out.printf("Station %d: granted access to conveyor %d.\n", this.id, conveyor);
  49.     }
  50.  
  51.     //Function to release access to conveyor
  52.     public void releaseAccesss(int conveyor)
  53.     {
  54.         System.out.printf("Station %d: released access to conveyor %d.\n", this.id, conveyor);
  55.     }
  56.  
  57.     //Station completes workload
  58.     public void completeWorkload()
  59.     {
  60.         System.out.printf("* * Station %d: Workload successfully completed. * *\n", this.id);
  61.     }
  62.  
  63.     //A station successfully flows packages down a conveyor[calls doWork method]
  64.     public void doWork(int conveyor)
  65.     {
  66.         System.out.printf("Station %d: successfully moves packages on conveyor %d.\n", this.id, conveyor);
  67.     }
  68.  
  69.     //A station completes a flow
  70.     public void completeFlow()
  71.     {
  72.         System.out.printf("Station %d: has %d package groups left to move.\n", this.id, this.workload);
  73.     }
  74.  
  75. }
  76.  
  77. public class Main {
  78.  
  79.     public static void main(String[] args) {
  80.         //Total number of Routing Station
  81.         int RS;
  82.  
  83.         //Declaring scanner class to input from config file
  84.         Scanner inFile = null;
  85.         try
  86.         {
  87.              inFile = new Scanner(new FileReader("config.txt"));
  88.         }
  89.         catch (FileNotFoundException e)
  90.         {
  91.             System.out.println("Input config file not found");
  92.         }
  93.         //Inputting the number of routing station
  94.         RS = inFile.nextInt();
  95.  
  96.         //Array for holding workload for routing station
  97.         Station st[] =  new Station[RS];
  98.  
  99.         //Declaring reentrant lock for each conveyor
  100.         ReentrantLock conveyorLock[] = new ReentrantLock[RS];
  101.         //Initialize all conveyorLock to unlock mode
  102.         for (int i = 0; i < RS; i++) {
  103.             conveyorLock[i] = new ReentrantLock();
  104.             conveyorLock[i].unlock();
  105.         }
  106.  
  107.         //Inputting workload for routing station
  108.         for (int i = 0; i < RS; i++) {
  109.             st[i] = new Station(i, inFile.nextInt());
  110.         }
  111.  
  112.         //Running all invidual thread
  113.         for (int i = 0; i < RS; i++)
  114.         {
  115.             int temp = i;
  116.             new Thread(new Runnable() {
  117.                     @Override
  118.                     public void run() {
  119.                         //Setting incoming connection to conveyor
  120.                         st[temp].inConnectionSet(temp);
  121.  
  122.                         //Setting outgoing connection to conveyor
  123.                         if(temp - 1 < 0)
  124.                             st[temp].outConnectionSet(RS - 1);
  125.                         else
  126.                             st[temp].outConnectionSet(temp - 1);
  127.  
  128.                         //Setting Workload
  129.                         st[temp].workloadSet();
  130.  
  131.                         //======================================================================//
  132.                         //If conveyor is unavailable wait 500 milliseconds
  133.                         while(conveyorLock[temp].isLocked()) {
  134.                             try {
  135.                                 wait(500);
  136.                             } catch (InterruptedException e) {
  137.                                 e.printStackTrace();
  138.                             }
  139.                         }
  140.                             //Getting exclusive access to conveyor
  141.                         conveyorLock[temp].lock();
  142.                         //Putting unlock statement to finally block so that it still runs in case of exception
  143.                         try{
  144.                            
  145.                         }
  146.                         finally {
  147.                             conveyorLock[temp].unlock();
  148.                         }
  149.                         //======================================================================//
  150.  
  151.                         //Thread sleeps for a random period of time[between 200 to 1000 milliseconds]
  152.                         Random r = new Random();
  153.                         try {
  154.                             sleep(r.nextInt(801)+200);
  155.                         } catch (InterruptedException e) {
  156.                             e.printStackTrace();
  157.                         }
  158.                     }
  159.                 });
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement