我环顾四周,似乎是类似的代码,但是我的代码无法正常工作。我的volatile变量正在更改班级时钟,但我的班级访问者未获取更改后的变量。我将发布我的代码。如果有类似问题,请链接。感谢您的帮助。

我尝试通过将所有类中的volatile布尔变量的声明设置为false来进行尝试。它没有帮助。

public class Main {
volatile static boolean isInSession;
volatile static boolean sessionOver;

public static void main (String [] args)
{
    for (int i = 0; i < 25; i++) {
        Visitor visitor = new Visitor(i, isInSession);
        visitor.start();
    }
    Thread clock = new Thread(new Clock(isInSession, sessionOver));
    clock.start();

}



}



public class Visitor extends Thread {


volatile static boolean isInSession;
private int visitorId;
volatile static int seats = 5;

Visitor(int visotrId, boolean isInSession)
{
    this.isInSession = isInSession;
    setName("visitorId " + visitorId);

}
@Override
public void run() {

    while(true)
    {
        while(isInSession){}

        System.out.println("In visitor isInSession " + isInSession);

        if(isInSession)
            System.out.println("Visitor isInSession " + isInSession);

        try {
            Thread.currentThread().sleep(5000);
        }
        catch(InterruptedException e)
        { }

    }
}
public void msg(String m) {
System.out.println("["+(System.currentTimeMillis()-time)+"]
"+getName()+": "+m);
}


}


public class Clock implements Runnable  {

volatile static boolean isInSession;
volatile static boolean sessionOver;
private int session = 0;

public Clock(boolean isInSession, boolean sessionOver)
{
    this.isInSession = isInSession;
    this.sessionOver = sessionOver;

}

@Override
public void run() {
    while(true)
    {
        try {
            Thread.currentThread().sleep(5000);
        } catch (InterruptedException e) {
        }
        isInSession = false;

        msg("Theater is open");

        try {
            Thread.currentThread().sleep(5000);
        } catch (InterruptedException e) {
        }

        isInSession = true;
        //System.out.println("In clock isInSession " + isInSession);


        session++;
    }
}// end of run

    public void msg(String m) {
    System.out.println("["+(System.currentTimeMillis()-time)+"]" +"Clock:
    "+  m);
}

}

最佳答案

您可以将AtomicBoolean用于您的目的。

正如JB Nizet指出的那样,Java中的参数是按值传递的。这是另一个SO post的答案,详细解释了这一点。

出于您的目的,只要知道“当我们传递对象的值时,我们就传递对它的引用”(来自上述SO帖子的引言)就足够了。通过创建AtomicBoolean对象并将其传递给ClockVisitor对象,当Clock更新AtomicBoolean的值时,Visitor对象也将接收更新的值。

因此,您的主类应如下所示:

public class Main {
    static AtomicBoolean isInSession = new AtomicBoolean(); // default value is false
    static AtomicBoolean sessionOver = new AtomicBoolean();

    public static void main (String [] args)
    {
        for (int i = 0; i < 25; i++) {
            Visitor visitor = new Visitor(i, isInSession);
            visitor.start();
        }
        Thread clock = new Thread(new Clock(isInSession, sessionOver));
        clock.start();
    }
}


要访问AtomicBooleanVisitor的值或更新Clock中的值,可以分别使用get()set(boolean)方法。

09-26 08:56