The 7 Dwarfs and the Diamond Mine Write a CONCURRENT program simulating the 7 dw
ID: 3576295 • Letter: T
Question
The 7 Dwarfs and the Diamond Mine Write a CONCURRENT program simulating the 7 dwarfs going to a diamond mine to collect treasures and bring them home. The program will use Java concurrency and threads to simulate the 7 dwarfs working together in PARALLEL to get all the diamond pieces from the mine. (Each dwarf should be a separate thread of execution working to collect diamonds from a shared resource (Diamonds)) There are 1,000 diamonds in the mine Your dwarfs live in a hamlet roughly 10 miles from the mine. However, the dwarfs take the trip at different speeds each time they head out. Simulate this by putting the thread to sleep from 5-10 seconds at the beginning of the diamond collection method. Each dwarf can carry from 10-50 diamond pieces per load (randomize this) When the dwarfs get home, they should off load the diamonds, and then sleep for 10-20 seconds before heading back out to the mine. (Dwarfs don't need much rest!!) Your program should report the following Each time a dwarf leaves the hamlet (and dwarf name) When a dwarf arrives at the mine (and name) When a dwarf arrives back home and how many diamonds he's carrying When a dwarf arrives home also print out how many diamonds are left in the mine Continue the simulation until all the pieces have been delivered home, then report: "all Diamonds have been collected" Also, report how many diamonds each dwarf (thread) collected.Explanation / Answer
import java.util.Random;
/**
* @author zubaeyr
*
*/
public class Dwarf implements Runnable {
private String name;
private int diamonds;
private Random randomNumberGenerator = new Random();
public Dwarf(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDiamonds() {
return diamonds;
}
public void setDiamonds(int diamonds) {
this.diamonds = diamonds;
}
@Override
public void run() {
while (DiamondMine.getDiamondCount() > 0) {
System.out.println(getName()+" left the home");
goToMine();
System.out.println(getName()+" arrived at the mine");
int fetchedDiamonds = extractDiamonds();
returnHome();
System.out.println(getName()+" returned home with "+fetchedDiamonds+" diamonds");
System.out.println("Remaining diamonds in the mine are "+DiamondMine.getDiamondCount());
unloadDiamonds(fetchedDiamonds);
takeRest();
}
}
public void goToMine() {
/*
* May take random time between 5 to 10 seconds
* to reach the mine
* */
int timeToTravel = 6 + randomNumberGenerator.nextInt(5);
try {
Thread.sleep(1000 * timeToTravel);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public int extractDiamonds() {
/*
* Each Dwarf can carry upto 10-50 diamonds at a time
* */
int diamondsToExtract = 11 + randomNumberGenerator.nextInt(40);
return DiamondMine.extractDiamonds(diamondsToExtract);
}
public void returnHome() {
/* simulating the return journey*/
int timeToTravel = 6 + randomNumberGenerator.nextInt(5);
try {
Thread.sleep(1000 * timeToTravel);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void unloadDiamonds(int fetchedDiamonds) {
diamonds += fetchedDiamonds;
}
public void takeRest() {
/* simulating the return journey*/
int sleepTime = 11 + randomNumberGenerator.nextInt(10);
try {
Thread.sleep(1000 * sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* @author zubaeyr
*
*/
public class DiamondMine {
private static int totalDiamonds = 1000;
private static final Object lock = new Object();
public static int extractDiamonds(int n) {
int returnCount = 0;
/*
* Synchronizing the totalDiamonds,
* so that only one dwarf-thread can modify it at a time
* */
synchronized (lock) {
if (totalDiamonds >= n) {
returnCount = n;
totalDiamonds = totalDiamonds - n;
}
else {
returnCount = totalDiamonds;
totalDiamonds = 0;
}
}
return returnCount;
}
public static int getDiamondCount() {
int diamondCount = 0;
synchronized (lock) {
diamondCount = totalDiamonds;
}
return diamondCount;
}
}
public class Simulator {
public Simulator() {
/* Runnable implementation for each dwarf */
Dwarf iDwarfOne = new Dwarf("Happy");
Dwarf iDwarfTwo = new Dwarf("Sleepy");
Dwarf iDwarfThree = new Dwarf("Bashful");
Dwarf iDwarfFour = new Dwarf("Grumpy");
Dwarf iDwarfFive = new Dwarf("Dopey");
Dwarf iDwarfSix = new Dwarf("Sneezy");
Dwarf iDwarfSeven = new Dwarf("Doc");
/* Creating seperate threads */
Thread dwarfOne = new Thread(iDwarfOne);
Thread dwarfTwo = new Thread(iDwarfTwo);
Thread dwarfThree = new Thread(iDwarfThree);
Thread dwarfFour = new Thread(iDwarfFour);
Thread dwarfFive = new Thread(iDwarfFive);
Thread dwarfSix = new Thread(iDwarfSix);
Thread dwarfSeven = new Thread(iDwarfSeven);
/* Start all the threads */
dwarfOne.start();
dwarfTwo.start();
dwarfThree.start();
dwarfFour.start();
dwarfFive.start();
dwarfSix.start();
dwarfSeven.start();
/* Wait until all the threads have completed their execution */
try {
dwarfOne.join();
dwarfTwo.join();
dwarfThree.join();
dwarfFour.join();
dwarfFive.join();
dwarfSix.join();
dwarfSeven.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("All Diamonds have been collected!");
System.out.println(iDwarfOne.getName()+" : "+iDwarfOne.getDiamonds());
System.out.println(iDwarfTwo.getName()+" : "+iDwarfTwo.getDiamonds());
System.out.println(iDwarfThree.getName()+" : "+iDwarfThree.getDiamonds());
System.out.println(iDwarfFour.getName()+" : "+iDwarfFour.getDiamonds());
System.out.println(iDwarfFive.getName()+" : "+iDwarfFive.getDiamonds());
System.out.println(iDwarfSix.getName()+" : "+iDwarfSix.getDiamonds());
System.out.println(iDwarfSeven.getName()+" : "+iDwarfSeven.getDiamonds());
}
public static void main(String args[]) {
new Simulator();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.