-1

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

Jamieden
  • 11
  • 3

1 Answers1

0

That you see the threads work serially could be because the threads don't have much to do and starting threads needs some time.

It is therefore plausible that thread 0 finishes all of its work before thread 1 and 2 had enough time to start.

To get better thread interleaving you could add artificial delays (Thread.sleep(10);) into the while loop in the station.run() method and into the work() method.


With these delays in place you will notice that your attempt at synchronization doesn't work.

For one thing, the boolean fields C0 to C4 are instance fields of the stations. When the first station sets its 'C0' field to true the other stations will never notice it because they check their own instance fields only.

You can make all the stations access the same fields C0 to C4 by declaring them as private static volatile boolean.

However your synchronization will still not work. Using volatile doesn't prevent that two threads read C0 as false at the same time and set it to true shortly thereafter (there is always a small time window between the read of the field and the following write).


If you must invent your own synchronization then you need at least AtomicBoolean - this class lets you change its value from false to true in an atomic way that no other thread can disrupt.

The question is whether you must invent your own synchronization or whether you are allowed to use classes like java.util.concurrent.locks.ReentrantLock, java.util.concurrent.locks.ReentrantReadWriteLock or java.util.concurrent.Semaphore.


And, before I forget: you must read How do I compare strings in Java?. If you continue to compare strings with == your code will break in seemingly inexplicable ways.

Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34