For some reason i have the feeling that i should put all of my thread starts and joins in a for loop just to reduce the amount of code. I dunno if that will hurt readability on this class? Also is it normal for a main class to be initialize just to call one method? I only did this since I was getting an error regarding static vs non static.
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
public class Main {
public static String[] table;//Table for the ingredients
public static void main(String[] args) {
//Gets the number of iterations from the user then maybe pass it to the agent who will wait/notifty until the loop is over
Main main = new Main();
main.runThreads();
}
public void runThreads(){
int numofTests;
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of iterations to be completed:");
numofTests = Integer.parseInt(in.nextLine());///Gets the number of tests from the user
Agent agent = new Agent(numofTests);
Smoker Pat = new Smoker ("paper", "Pat");
Smoker Tom = new Smoker ("tobacco", "Tom");
Smoker Matt = new Smoker ("matches", "Matt");
for (int i = 0; i < numofTests; i++){
Thread thread1 = new Thread(Pat);
Thread thread2 = new Thread(Tom);
Thread thread3 = new Thread(Matt);
Thread thread4 = new Thread(agent);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
try {
thread1.join();
thread2.join();
thread3.join();
thread4.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
Smoker.latch = new CountDownLatch(1);//Resets the countdown latch for the smokers
System.out.println("*********************");
}
System.out.println("*********************");
System.out.println("Pat has smoked a total of " + Pat.numOfSmokes);
System.out.println("Matt has smoked a total of " + Matt.numOfSmokes);
System.out.println("Tom has smoked a total of " + Tom.numOfSmokes);
}
}