I am supposed to write code for an assignment. The goal is to make 2 threads (which are objects of a class that implements runnable) take turns printing the alphabet. One of them prints upper case while the other one prints lower case. (they print only a single letter each turn, not the whole alphabet, just for clarification)
I feel like my code is pretty self-explainatory but if I am wrong here and you have questions please ask them! I appreciate any help I can get for sure!
The Code:
public class ABCPrinter implements Runnable {
// --- Attributes ---
private boolean bool_isUpperCase;
public static boolean bool_Switch = true;
// --- Constructor ---
public ABCPrinter (boolean init_isUpperCase) {
this.bool_isUpperCase = init_isUpperCase;
}
@Override
public synchronized void run() { // custom run method
for (char char_Counter = 'a'; char_Counter <= 'z'; char_Counter++) { // count through the alphabet
if (bool_isUpperCase){ // decide whether to print upper or lower case
if(bool_Switch) {
System.out.println(Character.toUpperCase(char_Counter));
System.out.println("\n----------------------");
System.out.println("Message has been sent.");
System.out.println("-----------------------");
try {
Thread.sleep(1000);
} catch(Exception e) {
System.out.println("\nInterrupted.");
}
bool_Switch = false;
System.out.println("\n--------------------");
System.out.println("Switch has been set to false.");
System.out.println("-----------------------");
try {
Thread.sleep(1000);
notifyAll();
System.out.println("\n--------------------");
System.out.println("All threads have been notified.");
System.out.println("-----------------------");
Thread.sleep(1000);
System.out.println("\n--------------------");
System.out.println("Thread 1 is entering waiting state.");
System.out.println("-----------------------");
wait();
} catch (Exception e) {
System.out.println("Process Interrupted.");
}
} else {
try {
System.out.println("Thread 1 is waiting.");
wait();
} catch (Exception e) {
System.out.println("Process Interrupted.");
}
}
} else {
if(!bool_Switch) {
System.out.println(Character.toUpperCase(char_Counter));
System.out.println("\n----------------------");
System.out.println("Message has been sent.");
System.out.println("-----------------------");
try {
Thread.sleep(1000);
} catch(Exception e) {
System.out.println("\nInterrupted.");
}
bool_Switch = true;
System.out.println("\n--------------------");
System.out.println("Switch has been set to true.");
System.out.println("-----------------------");
try {
Thread.sleep(1000);
notifyAll();
System.out.println("\n--------------------");
System.out.println("All threads have been notified.");
System.out.println("-----------------------");
Thread.sleep(1000);
wait();
System.out.println("\n--------------------");
System.out.println("Thread 2 is entering waiting state.");
System.out.println("-----------------------");
} catch (Exception e) {
System.out.println("Process Interrupted.");
}
} else {
try {
System.out.println("Thread 2 is waiting.");
wait();
} catch (Exception e) {
System.out.println("Process Interrupted.");
}
}
}
}
}
}
The main where I execute everything:
public class Main2 {
public boolean bool_switch;
public static void main(String[] args){
ABCPrinter p1 = new ABCPrinter(true);
ABCPrinter p2 = new ABCPrinter(false);
Thread thr_UpperCase = new Thread(p1);
Thread thr_LowerCase = new Thread(p2);
thr_UpperCase.start();
thr_LowerCase.start();
}
}