允许在Runnable对象中进行更改

时间:2014-10-23 10:57:49

标签: java multithreading runnable

当我将runnable放入Thread之后我更改了一个可运行的对象时,它不再获得更改。

例如:

主级:

//Data objects
private GreenhouseBO g;
//Greenhouse-thread
private GreenhouseRunnable greenhouseRunnable;
private Thread greenhouseThread;

public static void main(String[] args) {
        //Make greenhouse object
        g = new GreenhouseBO("192.168.0.100", 1234);

        //Make runnable
        greenhouseRunnable = new GreenhouseRunnable();
        greenhouseRunnable.addGreenhouse(g);

        //Make thread
        greenhouseThread = new Thread(greenhouseRunnable);
        greenhouseThread.start();

        //Now greenhouseRunnable's run while loop will print GreenhouseBO's address every second.
        //But when I change the GreenhouseBO object, it doesn't go through to the runnable.. It still prints the "old" greenhouse-objects data...
        System.out.println("Setting port....");
        g.setPort(5678);

        /*
        Actual output:
        Address: 192.168.0.100:1234
        Address: 192.168.0.100:1234

        Expected output:
        Address: 192.168.0.100:1234
        Address: 192.168.0.100:5678
        */

}

GreenhouseBO.java:

public class GreenhouseBO extends Observable {
    private int temp_in;
    private String ip;
    private int port;

    public GreenhouseBO(String ip, int port) {
        this.ip = ip;
        this.port = port;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public int getPort() {
        return port;
    }

    public int getTemp_in() {
        return temp_in;
    }

    public void setTemp_in(int temp_in) {
        this.temp_in = temp_in;
        setChanged();
        notifyObservers();
    }

}

GreenhouseRunnable.java:

public class GreenhouseRunnable implements Runnable {
    private GreenhouseBO g; //Business-objekt

    @Override
    public void run() {
        while (true) {
            System.out.println("Address: " + g.getIp() + ":" + String.valueOf(g.getPort()));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(GreenhouseRunnable.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void addGreenhouse(GreenhouseBO g){
        if(g == null){
            this.g = g;
        }
    }

    public void clearGreenhouse(){
        this.g = null;
    }
}

我如何才能完成这项工作,以便我可以更改我的GreenhouseBO对象,并且可以运行"识别" GreenhouseBO已发生变化。

1 个答案:

答案 0 :(得分:3)

首先,您永远不会在GreenhauseBO

中设置此GreenhouseRunnable
public void addGreenhouse(GreenhouseBO g){
    if(g == null){ // g refers to the parameter
        this.g = g;
    }
}

应该是

public void addGreenhouse(GreenhouseBO g){
    if(this.g == null){
        this.g = g;
    }
} 

其次...

在您的代码中,您将GreenhouseBO分配给变量g,但您的代码仅显示实例变量g。由于您在静态上下文static void main中进行赋值,因此不应编译。是否还有更多未发布的代码。

private GreenhouseBO g;

public static void main(String[] args) {
    //Make greenhouse object
    g = new GreenhouseBO("192.168.0.100", 1234); // where is the static `g` variable

或者你的意思是

private static GreenhouseBO g;