From b4f7e6f91b5fe8fe6b8cec2dfcbc69c945a296df Mon Sep 17 00:00:00 2001 From: elliotbrandwein Date: Tue, 19 Apr 2016 15:47:23 -0400 Subject: [PATCH 1/2] Added files via upload This will now ask for treasure size, it will terminate 90% of the time. It still can't have more then 1 clerk --- Adventurer.java | 22 ++++++++++---- Clerk.java | 3 +- Dragon.java | 2 +- MainThread.java | 76 +++++++++++++++++++++++++++++++++++-------------- 4 files changed, 74 insertions(+), 29 deletions(-) diff --git a/Adventurer.java b/Adventurer.java index 2a468f0..e6fa53e 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)); @@ -81,6 +82,7 @@ private synchronized void joinAdventurers() throws InterruptedException { if(mainThread.stillLiveAdventurer()) { + msg("has joined another adventurer"+"\n"); mainThread.getAliveAdventurer().join(); } } @@ -120,14 +122,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..a12d895 100644 --- a/Clerk.java +++ b/Clerk.java @@ -24,7 +24,7 @@ public void run() { helpCustomers(); } - msg("has terminated because there are no more adventurers"); + msg("has terminated because there are no more adventurers"+"\n"); } @@ -34,6 +34,7 @@ private synchronized void helpCustomers() { msg("is about to help a waiting customer"); mainThread.getNextInShopLine().getAssistance(); + msg("has helped the customer"); } diff --git a/Dragon.java b/Dragon.java index f06a3de..e340a79 100644 --- a/Dragon.java +++ b/Dragon.java @@ -25,7 +25,7 @@ public void run() { } - msg("is done"); + msg("is done"+"\n"); } /* private Adventurer pickPlayer() diff --git a/MainThread.java b/MainThread.java index b58e120..4637fcb 100644 --- a/MainThread.java +++ b/MainThread.java @@ -9,6 +9,7 @@ public class MainThread extends Thread{ private Clerk[] clerks; private Adventurer[] adventurers; private Boolean[] aliveAdventurers; + private Boolean[] need_assistance; private LinkedQueue shopLine = new LinkedQueue(); private Dragon dragon; private boolean shopLineLock=false; @@ -30,11 +31,13 @@ public MainThread() 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_fortuneSize=getInput("whats the 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]; + 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 @@ -43,6 +46,7 @@ public MainThread() throws Exception { adventurers[i]= new Adventurer(i,num_fortuneSize,this); aliveAdventurers[i]=true; + need_assistance[i]=false; } for(int i =0; i Date: Tue, 19 Apr 2016 16:59:09 -0400 Subject: [PATCH 2/2] Added files via upload This will now run 99% of the time. There is an ugly hack that will allow for 2 clerks to run. the input is also now done via command line arguments instead of scanner. Lastly, I have added the use of the isAliveMethod so that the dragon and clerk will terminate after all the adventurers. --- Adventurer.java | 1 - Clerk.java | 6 +++- Dragon.java | 2 +- Main.java | 39 +++++++++++++++++++++- MainThread.java | 86 +++++++++++++++++++++++++++---------------------- 5 files changed, 91 insertions(+), 43 deletions(-) diff --git a/Adventurer.java b/Adventurer.java index e6fa53e..4e95b75 100644 --- a/Adventurer.java +++ b/Adventurer.java @@ -72,7 +72,6 @@ public void run() try { joinAdventurers(); } catch (InterruptedException e) { - // TODO Auto-generated catch block e.printStackTrace(); } msg("is done "+"\n"); diff --git a/Clerk.java b/Clerk.java index a12d895..0578b2f 100644 --- a/Clerk.java +++ b/Clerk.java @@ -20,7 +20,7 @@ public Clerk(int id, MainThread parentThread) throws Exception public void run() { msg("has been made"); - while(mainThread.stillLiveAdventurer()) + while(mainThread.checkForLivingThreads()) { helpCustomers(); } @@ -33,7 +33,11 @@ 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 e340a79..0fb0b08 100644 --- a/Dragon.java +++ b/Dragon.java @@ -21,7 +21,7 @@ public Dragon(MainThread parentThread) public void run() { msg("has been made"); - while(mainThread.stillLiveAdventurer()) + while(mainThread.checkForLivingThreads()) { } 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 4637fcb..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,14 +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() @@ -26,17 +27,21 @@ 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_fortuneSize=getInput("whats the fortune size?"); + //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 @@ -45,7 +50,7 @@ public MainThread() throws Exception for(int i =0; i