如何在多线程中并行启动Java个线程?

How to start Java threads in multithreading in parallel?

我目前正在做一个项目,它迫使我不能使用 Java 的内置同步方法,但是,我无法让项目彼此并行地进入 运行 .它应该让其他线程尝试锁定传送带并可能失败,因为它需要的那个已经被锁定,但它们只是 运行 就好像它们不是多线程一样。我做错了什么?

代码:

/*
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();
        }
    }

}

输出:

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!

预期输出: Output Expectation

您看到线程连续工作可能是因为线程没有太多工作要做,启动线程需要一些时间。

因此,线程 0 在线程 1 和 2 有足够的时间启动之前完成其所有工作是合理的。

为了获得更好的线程交错,您可以在 station.run() 方法和 work() 方法的 while 循环中添加人为延迟 (Thread.sleep(10);)。


有了这些延迟,您会发现您的同步尝试不起作用。

一方面,布尔字段C0C4是站点的实例字段。当第一个站点将其 'C0' 字段设置为 true 时,其他站点永远不会注意到它,因为它们只检查自己的实例字段。

您可以通过将它们声明为 private static volatile boolean.

让所有站访问相同的字段 C0C4

但是您的同步仍然无法进行。使用 volatile 并不能防止两个线程同时将 C0 读取为 false 并在之后不久将其设置为 true (两者之间总是有一小段时间 window读取该字段并进行后续写入)。


如果您必须发明自己的同步,那么您至少需要 AtomicBoolean - 这个 class 允许您以原子方式将其值从 false 更改为 true没有其他线程可以中断。

问题是您是否必须发明自己的同步,或者您是否被允许使用 classes,例如 java.util.concurrent.locks.ReentrantLockjava.util.concurrent.locks.ReentrantReadWriteLockjava.util.concurrent.Semaphore


并且,在我忘记之前:您必须阅读How do I compare strings in Java?。如果您继续将字符串与 == 进行比较,您的代码将以看似无法解释的方式中断。