There is this task that was given to me to do and I have tried all my best but couldn’t be able to achieve it. The task is to write a Java Program that will produce a pancake and at the same time users will be picked at random to eat the produced pancakes. The two things must be done concurrently, meaning that as the pancake is being produced, users will check if there is any available pancake produced and then eat it. Each user is expected to eat a maximum of 5 pancakes and the producer of the pancake is expected to produce a maximum of 12 pancakes within the given time. The whole of this thing is to happen within only 20 seconds and then the program should be terminated and print out the report after the 20 seconds. The Instruction was also that I should make the two things to run concurrently but not to use Thread or ExecutorService. I really need help some help on how to achieve it. Here is what I tried and I am getting an infinite loop on the producePancake method, it doesn’t even allow the eatPancake method to execute.
public class PanCakeTask {
int consumedPancake = 0;
int availablePancake = 0;
int totalPancakeProduced = 0;
List<String> usersList = new ArrayList<>();
void producePancake(HashMap<String,Integer>users){
// first check the total pancake produced so far by checking the number
// of pancakes eaten by the users plus the available ones
for (String user: users.keySet()){
consumedPancake +=users.get(user);
}
totalPancakeProduced += consumedPancake + availablePancake;
while (totalPancakeProduced <12){
//only continue with the production of pancakes if the number of pancakes
//produced has not been exhausted (i.e 15)
System.out.println("Pancake produced");
//increment the number of pancakes available
availablePancake +=1;
}
}
void eatPancake(HashMap<String,Integer>users){
//first pick the user at random
Collections.shuffle(usersList);
int numberEaten = users.get(usersList.get(0));
//get the user that is on index zero after the list has been shuffled
//then check how many pancake that the picked user has eaten
//because each user is expected to eat a maximum of 5 pancake
if (numberEaten ==5){
//This means that this user has exhausted his/her quota so he/she cannot
//be allowed to eat again
//In this case, the eatPancake method is called again to randomly pick another user
//I am thinking of removing the user from the list here so that he/she cannot be picked again
eatPancake(users);
}else {
//Meaning that the user still has some quota left for him/her to eat
//In this other condition, it will check if there is an available pancake produced
if (availablePancake >=1){
//user will now eat the pancake
//then increment the number of pancake eaten by that user
// and decrement the number of pancakes available
int newCount = numberEaten + 1;
availablePancake -=1;
//finally update the user that just eaten the pancake
users.put(usersList.get(0),newCount);
System.out.println("Eaten a pancake");
}
}
}
public void performTask(){
//Hashmap is used to store the users because of key-value pairs
HashMap <String,Integer> users = new HashMap<>();
users.put("user1",0);
users.put("user2",0);
users.put("user3",0);
usersList.addAll(users.keySet());
System.out.println(users);
System.out.println(usersList);
//This task can only be executed in 20 seconds
long startTimeInSeconds = System.currentTimeMillis()/1000;
long requiredTime = startTimeInSeconds +20;
while (startTimeInSeconds !=requiredTime){
//only continue the process if it has not exceeded 20 seconds
System.out.println("Start time is "+startTimeInSeconds);
System.out.println("End time is "+requiredTime);
//This two method is to be executed concurrently without using Thread or ExecutorService
// I need help on how to achieve that without the use of Thread or ExecutorService
producePancake(users);
eatPancake(users);
startTimeInSeconds = System.currentTimeMillis()/1000;
}
System.out.println("Total number of cake produced is "+totalPancakeProduced);
System.out.println("Total number of cake eaten is "+consumedPancake);
System.out.println("Total number of pancake remained is "+availablePancake);
System.out.println("Time start is "+startTimeInSeconds);
System.out.println("Time completed is "+requiredTime);
}
}