diff --git a/Adventurer.java b/Adventurer.java index 2a468f0..4e95b75 100644 --- a/Adventurer.java +++ b/Adventurer.java @@ -15,14 +15,15 @@ public class Adventurer extends Thread { private static ArrayList need_assistance = new ArrayList(); private MainThread mainThread; private boolean isWaitingForDragon=false; + private int errorchek=0; //constructor will set the fortuneSize,adventurerId,stones,rings,chains,earring,mainThread, and need_assitance variables public Adventurer(int id, int fortuneSize, MainThread parentThread) throws Exception { - stones=getRandomInt()%1; - rings=getRandomInt()%1; - chains=getRandomInt()%1; - earrings=getRandomInt()%1; + stones=getRandomInt()%4; + rings=getRandomInt()%4; + chains=getRandomInt()%4; + earrings=getRandomInt()%4; adventurerId=id; need_assistance.add(false);// The assistance array initializes to false setName("Adventurer-"+(id+1)); @@ -71,7 +72,6 @@ public void run() try { joinAdventurers(); } catch (InterruptedException e) { - // TODO Auto-generated catch block e.printStackTrace(); } msg("is done "+"\n"); @@ -81,6 +81,7 @@ private synchronized void joinAdventurers() throws InterruptedException { if(mainThread.stillLiveAdventurer()) { + msg("has joined another adventurer"+"\n"); mainThread.getAliveAdventurer().join(); } } @@ -120,14 +121,22 @@ private synchronized void shop() { msg("has entered the shop"); need_assistance.set(adventurerId, true); + //mainThread.setAssistance(adventurerId); mainThread.joinShopLine(this); msg("is now busy-waiting"); - while(need_assistance.get(adventurerId)){} // this is the busy-wait, waiting for the clerk at the shop + while(need_assistance.get(adventurerId)){ + errorchek++; + if(errorchek%1000000==0)msg("is stuck in the busy wait"); + } // this is the busy-wait, waiting for the clerk at the shop + //while(mainThread.needAssistance(adventurerId)){} msg("has got help"); makeMagicItems(); msg("has left the shop"); } - public void getAssistance(){ need_assistance.set(adventurerId,false);} + public void getAssistance() + { + need_assistance.set(adventurerId,false); + } private void makeMagicItems() { diff --git a/Clerk.java b/Clerk.java index cc85782..0578b2f 100644 --- a/Clerk.java +++ b/Clerk.java @@ -20,11 +20,11 @@ public Clerk(int id, MainThread parentThread) throws Exception public void run() { msg("has been made"); - while(mainThread.stillLiveAdventurer()) + while(mainThread.checkForLivingThreads()) { helpCustomers(); } - msg("has terminated because there are no more adventurers"); + msg("has terminated because there are no more adventurers"+"\n"); } @@ -33,7 +33,12 @@ private synchronized void helpCustomers() if(mainThread.isShopLineEmpty()!=true) { msg("is about to help a waiting customer"); + try + { mainThread.getNextInShopLine().getAssistance(); + } + catch(Exception e){} + msg("has helped the customer"); } diff --git a/Dragon.java b/Dragon.java index f06a3de..0fb0b08 100644 --- a/Dragon.java +++ b/Dragon.java @@ -21,11 +21,11 @@ public Dragon(MainThread parentThread) public void run() { msg("has been made"); - while(mainThread.stillLiveAdventurer()) + while(mainThread.checkForLivingThreads()) { } - msg("is done"); + msg("is done"+"\n"); } /* private Adventurer pickPlayer() diff --git a/Main.java b/Main.java index 608421b..abaff54 100644 --- a/Main.java +++ b/Main.java @@ -3,7 +3,44 @@ public class Main{ // finally, all the main method does is create the Main class and start that thread public static void main(String[] args) throws Exception { - MainThread questAdventure = new MainThread(); + int num_adv=0; + int num_fortuneSize=0; + // if there are no arguments we use the defaults + if (args.length == 0) + { + num_adv = 6; + num_fortuneSize = 5; + } + else + { + // if there are arguments we check them and then use them if they are valid + try + { + if (args.length != 2) throw new IllegalArgumentException(); + } + catch (IllegalArgumentException e) + { + System.out.println(e); + System.exit(0); + } + try + { + num_adv = Integer.parseInt(args[0]); + num_fortuneSize = Integer.parseInt(args[1]); + } + catch (Exception e) + { + System.out.println("the argumets weren't integers"); + System.exit(0); + } + // make sure they are positive + if (num_adv < 1 || num_fortuneSize < 1) + { + System.out.println("the arguments weren't positive"); + System.exit(0); + } + } + MainThread questAdventure = new MainThread(num_adv,num_fortuneSize); System.out.println("we made a Main object and are about to start all the threads"); questAdventure.start(); } diff --git a/MainThread.java b/MainThread.java index b58e120..fca4a32 100644 --- a/MainThread.java +++ b/MainThread.java @@ -1,5 +1,6 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.InputMismatchException; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; @@ -8,13 +9,14 @@ public class MainThread extends Thread{ private Clerk[] clerks; private Adventurer[] adventurers; - private Boolean[] aliveAdventurers; + private Boolean[] adventurersThatStillNeedTreasure; + private Boolean[] need_assistance; private LinkedQueue shopLine = new LinkedQueue(); private Dragon dragon; private boolean shopLineLock=false; - private int num_adv=6; + private int num_adv=0; private int num_clerk=2; - private int num_fortuneSize=5; + private int num_fortuneSize=0; // the run method calls the start method on all the other threads the main thread has created public void run() @@ -25,16 +27,22 @@ public void run() return; } // this is the constructor for the Main class - public MainThread() throws Exception + public MainThread(int adv_num, int fortune_size) throws Exception { - //first we get the input from the user on how many clerks and adventurers we will be making - num_adv=getInput("how many adventurers do you want?"); - num_clerk=getInput("how many clerks do you want?"); + //num_adv=getInput("how many adventurers do you want?",0); + //num_clerk=getInput("how many clerks do you want?",1); + //num_fortuneSize=getInput("whats the fortune size?",2); + + num_adv=adv_num; + num_fortuneSize=fortune_size; + + // next we make the arrays where we will store are the clerk and adventurer threads clerks= new Clerk[num_clerk]; adventurers = new Adventurer[num_adv]; - aliveAdventurers= new Boolean[num_adv]; + adventurersThatStillNeedTreasure= new Boolean[num_adv]; + need_assistance = new Boolean[num_adv]; /* now we make all the clerks,adventurers and the dragon * the shared variables are stored in this class, so we pass it to each thread we make @@ -42,7 +50,8 @@ public MainThread() throws Exception for(int i =0; i