Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.util.Random;
- import java.util.Scanner;
- import java.util.concurrent.locks.ReentrantLock;
- import static java.lang.Thread.sleep;
- //Creating object for routing station
- class Station{
- //Declaring id and workload variable for routing station
- private int id;
- private int workload;
- //Declaring a variable for keeping track of aquired conveyor
- private int aquiredConveyor;
- //Creating a constructor
- public Station(int id, int workload)
- {
- this.id = id;
- this.workload = workload;
- this.aquiredConveyor = 0;
- }
- //Function to set incoming conveyor
- public void inConnectionSet(int conveyor)
- {
- System.out.printf("Station %d: In-Connection set to conveyor %d.\n", this.id, conveyor);
- }
- //Function to set outgoing conveyor
- public void outConnectionSet(int conveyor)
- {
- System.out.printf("Station %d: Out-Connection set to conveyor %d.\n", this.id, conveyor);
- }
- //Function to set workload
- public void workloadSet()
- {
- System.out.printf("Station %d: Workload set. Station %d has %d package groups to move.\n", this.id, this.id, this.workload);
- }
- //Function to grant access to conveyor
- public void grantAccess(int conveyor)
- {
- System.out.printf("Station %d: granted access to conveyor %d.\n", this.id, conveyor);
- }
- //Function to release access to conveyor
- public void releaseAccesss(int conveyor)
- {
- System.out.printf("Station %d: released access to conveyor %d.\n", this.id, conveyor);
- }
- //Station completes workload
- public void completeWorkload()
- {
- System.out.printf("* * Station %d: Workload successfully completed. * *\n", this.id);
- }
- //A station successfully flows packages down a conveyor[calls doWork method]
- public void doWork(int conveyor)
- {
- System.out.printf("Station %d: successfully moves packages on conveyor %d.\n", this.id, conveyor);
- }
- //A station completes a flow
- public void completeFlow()
- {
- System.out.printf("Station %d: has %d package groups left to move.\n", this.id, this.workload);
- }
- }
- public class Main {
- public static void main(String[] args) {
- //Total number of Routing Station
- int RS;
- //Declaring scanner class to input from config file
- Scanner inFile = null;
- try
- {
- inFile = new Scanner(new FileReader("config.txt"));
- }
- catch (FileNotFoundException e)
- {
- System.out.println("Input config file not found");
- }
- //Inputting the number of routing station
- RS = inFile.nextInt();
- //Array for holding workload for routing station
- Station st[] = new Station[RS];
- //Declaring reentrant lock for each conveyor
- ReentrantLock conveyorLock[] = new ReentrantLock[RS];
- //Initialize all conveyorLock to unlock mode
- for (int i = 0; i < RS; i++) {
- conveyorLock[i] = new ReentrantLock();
- conveyorLock[i].unlock();
- }
- //Inputting workload for routing station
- for (int i = 0; i < RS; i++) {
- st[i] = new Station(i, inFile.nextInt());
- }
- //Running all invidual thread
- for (int i = 0; i < RS; i++)
- {
- int temp = i;
- new Thread(new Runnable() {
- @Override
- public void run() {
- //Setting incoming connection to conveyor
- st[temp].inConnectionSet(temp);
- //Setting outgoing connection to conveyor
- if(temp - 1 < 0)
- st[temp].outConnectionSet(RS - 1);
- else
- st[temp].outConnectionSet(temp - 1);
- //Setting Workload
- st[temp].workloadSet();
- //======================================================================//
- //If conveyor is unavailable wait 500 milliseconds
- while(conveyorLock[temp].isLocked()) {
- try {
- wait(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- //Getting exclusive access to conveyor
- conveyorLock[temp].lock();
- //Putting unlock statement to finally block so that it still runs in case of exception
- try{
- }
- finally {
- conveyorLock[temp].unlock();
- }
- //======================================================================//
- //Thread sleeps for a random period of time[between 200 to 1000 milliseconds]
- Random r = new Random();
- try {
- sleep(r.nextInt(801)+200);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement