Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions Adventurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ public class Adventurer extends Thread {
private static ArrayList<Boolean> need_assistance = new ArrayList<Boolean>();
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));
Expand Down Expand Up @@ -71,7 +72,6 @@ public void run()
try {
joinAdventurers();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
msg("is done "+"\n");
Expand All @@ -81,6 +81,7 @@ private synchronized void joinAdventurers() throws InterruptedException
{
if(mainThread.stillLiveAdventurer())
{
msg("has joined another adventurer"+"\n");
mainThread.getAliveAdventurer().join();
}
}
Expand Down Expand Up @@ -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()
{
Expand Down
9 changes: 7 additions & 2 deletions Clerk.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}


Expand All @@ -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");

}

Expand Down
4 changes: 2 additions & 2 deletions Dragon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
39 changes: 38 additions & 1 deletion Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
158 changes: 100 additions & 58 deletions MainThread.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Adventurer> shopLine = new LinkedQueue<Adventurer>();
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()
Expand All @@ -25,24 +27,31 @@ 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
*/
for(int i =0; i<num_adv;i++)
{
adventurers[i]= new Adventurer(i,num_fortuneSize,this);
aliveAdventurers[i]=true;
adventurersThatStillNeedTreasure[i]=true;
need_assistance[i]=false;
}
for(int i =0; i<num_clerk;i++)
{
Expand All @@ -54,93 +63,107 @@ public MainThread() throws Exception
public void initThreads()
{
System.out.println("we are in the initThreads method");
for(int i=0;i<num_clerk;i++)
{
clerks[i].start();
}

for(int i=0; i<num_adv;i++)
{
adventurers[i].start();
}
for(int i=0;i<num_clerk;i++)
{
clerks[i].start();
}
dragon.start();
}

// this method takes in the user input for how many clerks they want or how many adventurers they want
private int getInput(String question) throws IOException
{
Scanner sc = new Scanner(System.in);
boolean getInput=true;
int output=0;
while(getInput)
{
System.out.print(question);
try
{
output=sc.nextInt();
}
catch (NumberFormatException e)
{
System.out.println("that wasn't a number, try that again");
}
if(output<1) System.out.println("that was too low of a number"+"\n");
else getInput=false;
public boolean needAssistance(int id){
boolean x;
synchronized(this){
x=need_assistance[id];
}
return output;
return x;
}


public synchronized boolean isShopLineEmpty()
public void setAssistance(int id)
{
shopLock();
boolean x = shopLine.isEmpty();
shopUnlock();
return x;
synchronized(this)
{
need_assistance[id]=true;
}
}
private synchronized void shopUnlock()
{
shopLineLock=false;
public void gotAssitance(int id){
synchronized(this)
{
need_assistance[id]=false;
}
}
private synchronized void shopLock()
public boolean isShopLineEmpty()
{
while(shopLineLock){}
shopLineLock=true;
boolean x;
synchronized(this)
{
x = shopLine.isEmpty();
}
return x;
}
public synchronized Adventurer getNextInShopLine()
public Adventurer getNextInShopLine()
{
shopLock();
Adventurer x= shopLine.dequeue();
shopUnlock();
//shopLock();
Adventurer x;
synchronized(this)
{
x= shopLine.dequeue();
}
//shopUnlock();
return x;
}
public synchronized void joinShopLine(Adventurer adv){
shopLock();
shopLine.enqueue(adv);
System.out.println(adv.getName() +" has entered the shop line");
shopUnlock();
public void joinShopLine(Adventurer adv){
//shopLock();
synchronized(this)
{
shopLine.enqueue(adv);
System.out.println(adv.getName() +" has entered the shop line");
}
//shopUnlock();
}
public synchronized boolean checkForLivingThreads(){
for(int i=0;i<num_adv;i++){
if(adventurers[i].isAlive()) return true;
}
return false;
}
public synchronized boolean stillLiveAdventurer()
{

for(int i=0; i<num_adv;i++)
{
if (aliveAdventurers[i]==true)return true;
if (adventurersThatStillNeedTreasure[i]==true)return true;
}
return false;
}
public void setAliveAdventurers (int i)
{
aliveAdventurers[i]=false;
adventurersThatStillNeedTreasure[i]=false;
}
public synchronized Adventurer getAliveAdventurer()
{
for(int i=1;i<num_adv;i++)
{
if(aliveAdventurers[i]) return adventurers[i];
if(adventurersThatStillNeedTreasure[i]) return adventurers[i];
}
return adventurers[0];
}

/*
*
private synchronized void shopUnlock()
{
shopLineLock=false;
}
private synchronized void shopLock()
{
while(shopLineLock){}
shopLineLock=true;
}

synchronized public void addToDragonLine(Adventurer adventurer)
{
dragonLock();
Expand Down Expand Up @@ -193,5 +216,24 @@ public void setPlayerJustLost(boolean x)
{
playerJustLost=x;
}

// this method takes in the user input for how many clerks they want or how many adventurers they want
private int getInput(String question, int version) throws IOException
{
Scanner sc = new Scanner(System.in);
boolean getInput=true;
int output=0;
while(getInput)
{
System.out.print(question);

output=sc.nextInt();
if(output<1) System.out.println("that was too low of a number"+"\n");
else getInput=false;
}
return output;
}


*/
}