All pastes #225359 Raw Edit

Untitled

public java v1 · immutable
#225359 ·published 2006-10-27 21:58 UTC
rendered paste body
import java.util.LinkedList;import java.util.Queue;import java.io.BufferedReader;import java.io.FileReader;import java.io.FileNotFoundException;import java.io.IOException;/** * Created by IntelliJ IDEA. * User: BC * Date: Oct 27, 2006 * Time: 5:12:17 PM * To change this template use File | Settings | File Templates. */public class prob_1 {    public static void main(String[] args) {        if(args.length != 2) {            System.out.println("Usage: java prob_1 <# of cashiers> <customer filename>");            System.exit(0);        }        System.out.println("Start");        int n = 0;        try {            n = Integer.parseInt(args[0]);        } catch(NumberFormatException e) {            System.out.println("The first argument must be an integer");            System.exit(0);        }        //TODO Checks for negative n <= 0        CustomerBag bag = new CustomerBag();        Thread cashier[] = new Thread[n];        for(int i=0; i<n; i++) {            cashier[i] = new Cashier(bag, i);            cashier[i].start();        }        Thread.yield();        BufferedReader in = null;        try {            in = new BufferedReader(new FileReader(args[1]));        } catch (FileNotFoundException e) {            System.out.println("The customer file does not exist");            System.exit(0);        }        String buff = null;        long startTime = System.currentTimeMillis();        try {            while((buff = in.readLine()) != null) {                String customerArgs[] = buff.split(" ");                if(customerArgs.length != 3) {                    System.out.println("Invalid syntax in customer file");                    in.close();                    System.exit(0);                }                long at = 0;                int rt = 0; // Arrival time and runtime                try {                    at = Integer.parseInt(customerArgs[1]) * 1000;  //Arrival time in ms                    rt = Integer.parseInt(customerArgs[2]);                } catch(NumberFormatException e) {                    System.out.println("Invalid syntax in customer file");                    in.close();                    System.exit(0);                }                long currTime = System.currentTimeMillis();                if(currTime - startTime < at) {                    try {                        Thread.sleep(at - (currTime - startTime));                    } catch (InterruptedException e) {                    }                }                System.out.println("Inserting " + customerArgs[0]);                bag.put(new Customer(customerArgs[0], rt));            }        } catch (IOException e) {            System.out.println("Error reading input - " + e.getMessage());            System.exit(0);        } finally {            try {                in.close();            } catch (IOException e) {            }        }        bag.done();        for(int i=0; i<cashier.length; i++) {            try {                cashier[i].join();            } catch (InterruptedException e) {            }        }        System.out.println("Done");    }    //TODO Print done}class Cashier extends Thread {    private CustomerBag bag;    public Cashier(CustomerBag b, int num) {        super("Thread " + num);        bag = b;    }    public void run() {        System.out.println(getName() + " begin");        Customer c = null;        while((c = bag.get()) != null) {            System.out.println(getName() + " " + c.getName() + " start");            try {                Thread.sleep(c.getRuntime());            } catch (InterruptedException e) {            }            System.out.println(getName() + " " + c.getName() + " finish");        }        System.out.println(getName() + " end");    }}class Customer {    private String name;    private int runtime;    public Customer(String n, int rt) {        name = n;        runtime = rt * 1000; //Runtime in ms    }    public String getName() {        return name;    }    public int getRuntime() {        return runtime;    }}class CustomerBag {    private Queue queue;    private boolean done;    public CustomerBag() {        queue = new LinkedList();        done = false;    }    synchronized void put(Customer o) {        queue.offer(o);        notify();    }    synchronized Customer get() {        while (!done && queue.isEmpty()) {            try {                wait();            } catch (InterruptedException e) {            }        }        if (done && queue.isEmpty()) {            return null;        }        return (Customer)queue.remove();    }    synchronized void done() {        done = true;        notifyAll();    }}