I'm trying to build a bank simulation using a queue, I got it to run but the results are not expected.
i'm trying to have clients adding to a queue and Tellers serving them running for 2 minutes. After Client 2, for some reason the tellers are not running anymore. I know it's still running in the background but not doing anything. Can someone points me the problem. Thanks.
HEre is the code
import java.util.*; import java.io.*; import java.util.concurrent.*; //now with Tellers class banker3 implements Runnable { public BlockingQueue<Client> bq = new LinkedBlockingQueue<Client>(); public static boolean stop = false; private int arrival; private static Random rand = new Random(); public banker3(BlockingQueue<Client> inq) { bq = inq; } public void run() { while(true) try{ Client c = new Client(); bq.add(c); arrival = rand.nextInt(5)+2; System.out.printf("Adding Client %s to the queue\n", c.cID); Thread.sleep(arrival*1000); if(stop) { System.out.printf("There are %d clients totaled.\n", Client.getTotal()); int remain = 0; while(bq.peek() != null){ Client temp = bq.remove(); remain++; } System.out.printf("There are %d clients remained in queue\n",remain); break; } } catch(InterruptedException e) { System.out.printf("Error\n",e); } } public static void main(String[] args) { BlockingQueue<Client>bankq = new LinkedBlockingQueue<Client>(); banker3 bank = new banker3(bankq); Thread queing = new Thread(bank); queing.setPriority(2); Teller t1 = new Teller(bankq,1); Teller t2 = new Teller(bankq,2); Teller t3 = new Teller(bankq,3); Teller t4 = new Teller(bankq,4); Teller t5 = new Teller(bankq,5); Thread tt1 = new Thread(t1); tt1.setPriority(2); Thread tt2 = new Thread(t2); tt2.setPriority(2); Thread tt3 = new Thread(t3); tt3.setPriority(2); Thread tt4 = new Thread(t4); tt4.setPriority(2); Thread tt5 = new Thread(t5); tt5.setPriority(2); StopWatch s = new StopWatch(); s.start();//start the timer queing.start(); tt1.start(); tt2.start(); tt3.start(); tt4.start(); tt5.start(); while(s.isRunning()) { if(s.elapSec() >= 120) { s.stop(); banker3.stop = true; Teller.stop = true; //System.out.printf("There are %d total clients\n", Client.getTotal()); } } } } //Client class class Client{ private static int total = 0; public int waitTime; int cID; int allocatedTo; private static Random rand = new Random(); public Client() { waitTime = rand.nextInt(4)+2; cID = ++total; allocatedTo = rand.nextInt(6); } public static int getTotal() { return total; } public static void setTotal() { total--; } } //Teller class class Teller implements Runnable { public BlockingQueue<Client>tq; private int totaltime; private int clientserved; public static boolean stop = false; public static boolean avail; private static int tot = 0; public static int servetime =0; int tID; public Teller(BlockingQueue<Client>iq, int id) { tq = iq; tID = id; avail = true; clientserved =0; totaltime=0; } public void run() { while(true) try { serve(); Thread.sleep(servetime); if(stop) { System.out.printf("Teller %d served %d clients and spent a total of %d secs\n", tID,clientserved,totaltime); break; } } catch(InterruptedException e) { System.out.printf("%s", e); } } public void serve() { Client temp = tq.peek(); if(temp != null && temp.allocatedTo == tID) { Client c = tq.remove(); servetime = c.waitTime; clientserved++; totaltime +=servetime; System.out.printf("Teller %d is serving client %d\n", tID, c.cID); } } } //stopwatch class class StopWatch { private long startTime = 0; private long stopTime = 0; private boolean running = false; public void start() { startTime = System.currentTimeMillis(); running = true; } public void stop() { stopTime = System.currentTimeMillis(); running = false; } //elaspsed time in milliseconds public long elapmili() { long elapsed; if (running) { elapsed = (System.currentTimeMillis() - startTime); } else { elapsed = (stopTime - startTime); } return elapsed; } public boolean isRunning() { return running; } //elaspsed time in seconds public long elapSec() { long elapsed; if (running) { elapsed = ((System.currentTimeMillis() - startTime) / 1000); } else { elapsed = ((stopTime - startTime) / 1000); } return elapsed; } }