I am currently working on a project that forces me to not use Java's built in synchronization method, however, I can't get the project to run in parallel with each other. It should let the other threads try to lock a conveyor and possibly fail because the one it needs is already locked, but they just run as if they weren't multithreaded. What am I doing wrong?
Code:
/*
Name: James Kitchens
Course: CNT 4714 Summer 2021
Assignment title: Project 1 - Multi-threaded programming in Java
Date: May 23, 2021
Class:
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class project1 {
//Number of routing stations
//Each line is for each stations workload integer
//Station 0: C4, C0
//Station 1: C0, C1
//Station 2: C1, C2
//Station 3: C2, C3
//Station 4: C3, C4
//TRICK! Just throw the programs out into the wild and don't synchronize the program. Just let them figure it out for themselves
//
public static void main(String[] args) throws FileNotFoundException {
File file = new File("config.txt");
Scanner scan = new Scanner(file);
String station = scan.nextLine();
int stations = Integer.valueOf(station);
int[] stationWork = new int[stations];
for(int i=0; i<stations; i++) {
String temp = scan.nextLine();
stationWork[i] = Integer.valueOf(temp);
}
if(stations == 3) {
station S0 = new station("0", "C0", "C1", stationWork[0]);
station S1 = new station("1", "C1", "C2", stationWork[1]);
station S2 = new station("2", "C2", "C0", stationWork[2]);
S0.start();
S1.start();
S2.start();
}
}
}
class station implements Runnable{
private Thread t;
private String threadName;
private volatile boolean C0 = false;
private volatile boolean C1 = false;
private volatile boolean C2 = false;
private volatile boolean C3 = false;
private volatile boolean C4 = false;
private String input;
private String output;
//private boolean inputLock = false;
//private boolean outputLock = false;
private int work;
station(String name, String input, String output, int work){
this.threadName = name;
this.input = input;
this.output = output;
this.work = work;
}
//System.out.println("Station " + threadName + ": ");
@Override
public void run() {
while(work > 0) {
if(input == "C0") {
if(output == "C1") {
if(C1 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C0 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C0 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C1 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C0 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C1 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
else if(output == "C2") {
if(C2 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C0 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C0 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C2 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C0 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C2 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
else if(output == "C3") {
if(C3 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C0 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C0 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C3 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C0 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C3 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
else if(output == "C4") {
if(C4 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C0 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C0 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C4 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C0 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C4 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
}
else if(input == "C1") {
if(output == "C0") {
if(C0 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C1 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C1 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C0 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C1 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C0 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
else if(output == "C2") {
if(C2 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C1 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C1 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C2 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C1 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C2 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
else if(output == "C3") {
if(C3 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C1 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C1 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C3 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C1 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C3 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
else if(output == "C4") {
if(C4 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C1 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C1 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C4 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C1 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C4 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
}
else if(input == "C2") {
if(output == "C1") {
if(C1 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C2 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C2 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C1 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C2 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C1 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
else if(output == "C0") {
if(C0 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C2 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C2 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C0 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C2 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C0 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
else if(output == "C3") {
if(C3 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C2 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C2 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C3 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C2 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C3 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
else if(output == "C4") {
if(C4 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C2 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C2 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C4 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C2 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C4 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
}
else if(input == "C3") {
if(output == "C1") {
if(C1 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C3 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C3 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C1 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C3 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C1 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
else if(output == "C2") {
if(C2 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C3 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C3 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C2 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C3 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C2 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
else if(output == "C0") {
if(C0 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C3 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C3 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C0 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C3 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C0 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
else if(output == "C4") {
if(C4 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C3 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C3 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C4 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C3 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C4 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
}
else if(input == "C4") {
if(output == "C1") {
if(C1 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C4 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C4 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C1 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C4 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C1 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
else if(output == "C2") {
if(C2 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C4 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C4 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C2 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C4 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C2 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
else if(output == "C3") {
if(C3 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C4 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C4 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C3 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C4 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C3 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
else if(output == "C0") {
if(C0 == true) {
System.out.println("Station " + threadName + ": Unable to lock output conveyor " + output + " - releasing lock on input conveyor " + input);
}
else if(C4 == true) {
System.out.println("Station " + threadName + ": Unable to lock input conveyor " + input + " - releasing lock on output conveyor " + output);
}
else {
C4 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on input conveyor " + input);
C0 = true;
System.out.println("Station " + threadName + ": LOCK ACQUIRED! Now holding lock on output conveyor " + output);
doWork();
C4 = false;
System.out.println("Station " + threadName + ": Unlocks input conveyor " + input);
C0 = false;
System.out.println("Station " + threadName + ": Unlocks output conveyor " + output);
}
}
}
}
System.out.println("** Station " + threadName + ": Workload successfully completed. ** Station Going Idle!");
t.stop();
}
public void doWork() {
System.out.println("Station " + threadName + ": ...Active... moving packages into station on input conveyor " + input);
System.out.println("Station " + threadName + ": ...Active... moving packages out of station on output conveyor " + output);
work--;
}
public void start() {
System.out.println("Starting station " + threadName);
if(t == null) {
t = new Thread(this, threadName);
t.start();
}
}
}
Output:
Starting station 0
Starting station 1
Station 0: LOCK ACQUIRED! Now holding lock on input conveyor C0
Station 0: LOCK ACQUIRED! Now holding lock on output conveyor C1
Station 0: ...Active... moving packages into station on input conveyor C0
Station 0: ...Active... moving packages out of station on output conveyor C1
Station 0: Unlocks input conveyor C0
Station 0: Unlocks output conveyor C1
Station 0: LOCK ACQUIRED! Now holding lock on input conveyor C0
Station 0: LOCK ACQUIRED! Now holding lock on output conveyor C1
Station 0: ...Active... moving packages into station on input conveyor C0
Station 0: ...Active... moving packages out of station on output conveyor C1
Station 0: Unlocks input conveyor C0
Station 0: Unlocks output conveyor C1
** Station 0: Workload successfully completed. ** Station Going Idle!
Starting station 2
Station 1: LOCK ACQUIRED! Now holding lock on input conveyor C1
Station 1: LOCK ACQUIRED! Now holding lock on output conveyor C2
Station 1: ...Active... moving packages into station on input conveyor C1
Station 1: ...Active... moving packages out of station on output conveyor C2
Station 1: Unlocks input conveyor C1
Station 1: Unlocks output conveyor C2
Station 1: LOCK ACQUIRED! Now holding lock on input conveyor C1
Station 1: LOCK ACQUIRED! Now holding lock on output conveyor C2
Station 1: ...Active... moving packages into station on input conveyor C1
Station 1: ...Active... moving packages out of station on output conveyor C2
Station 1: Unlocks input conveyor C1
Station 1: Unlocks output conveyor C2
Station 1: LOCK ACQUIRED! Now holding lock on input conveyor C1
Station 1: LOCK ACQUIRED! Now holding lock on output conveyor C2
Station 1: ...Active... moving packages into station on input conveyor C1
Station 1: ...Active... moving packages out of station on output conveyor C2
Station 1: Unlocks input conveyor C1
Station 1: Unlocks output conveyor C2
** Station 1: Workload successfully completed. ** Station Going Idle!
Station 2: LOCK ACQUIRED! Now holding lock on input conveyor C2
Station 2: LOCK ACQUIRED! Now holding lock on output conveyor C0
Station 2: ...Active... moving packages into station on input conveyor C2
Station 2: ...Active... moving packages out of station on output conveyor C0
Station 2: Unlocks input conveyor C2
Station 2: Unlocks output conveyor C0
Station 2: LOCK ACQUIRED! Now holding lock on input conveyor C2
Station 2: LOCK ACQUIRED! Now holding lock on output conveyor C0
Station 2: ...Active... moving packages into station on input conveyor C2
Station 2: ...Active... moving packages out of station on output conveyor C0
Station 2: Unlocks input conveyor C2
Station 2: Unlocks output conveyor C0
Station 2: LOCK ACQUIRED! Now holding lock on input conveyor C2
Station 2: LOCK ACQUIRED! Now holding lock on output conveyor C0
Station 2: ...Active... moving packages into station on input conveyor C2
Station 2: ...Active... moving packages out of station on output conveyor C0
Station 2: Unlocks input conveyor C2
Station 2: Unlocks output conveyor C0
Station 2: LOCK ACQUIRED! Now holding lock on input conveyor C2
Station 2: LOCK ACQUIRED! Now holding lock on output conveyor C0
Station 2: ...Active... moving packages into station on input conveyor C2
Station 2: ...Active... moving packages out of station on output conveyor C0
Station 2: Unlocks input conveyor C2
Station 2: Unlocks output conveyor C0
** Station 2: Workload successfully completed. ** Station Going Idle!
Expected Output: Output Expectation