While循环变量分配给Runnable

时间:2019-01-15 19:09:04

标签: java variables runnable final

为什么每个可运行的设备都应该分配相同的设备,但每个可运行的设备却打印相同的设备呢?

似乎每个Runnable都使用while循环中分配的最后一个设备。如何确保循环中为每个Runnable分配了一个设备?

Iterator<TaskCard> i = taskManager.getTaskCards().iterator();

while (i.hasNext()) {
    TaskCard taskCard = i.next();
    taskCard.updateTask();
    ReturnInterface<String> returnInterface = new TaskReturnIterface(taskManager, taskCard);
    Task task = taskCard.getTask();
    ProtocolInterface selectedProtocol = task.getDevice().getSelectedProtocol();
    selectedProtocol.setTask(task);
    selectedProtocol.setReturnInterface(returnInterface);

    SwingUtilities.invokeLater(new Runnable() {
        final ProtocolInterface mySelectedProtocol=selectedProtocol;
        @Override
        public void run() {  
            System.out.println("[Taskmanager.TaskReturnInterface.actionPerformed.RUN()]selectedProtocol device= " + mySelectedProtocol.getDevice());
        }
    });

}

下面是wsa请求的协议接口代码。

public abstract class ProtocolInterface<N> implements Callable<ReturnInterface<N>>, Serializable{

protected DefaultDevice device;
protected String name = "";
protected Task task;
protected Date scheduledDate;
protected ReturnInterface<N> returnInterface;

final private CredentialInterface credential = new CredentialInterface() {
    private String user = "";
    private String password = "";
    private int port = 22;

    @Override
    public String getUser() {
        return user;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public int getPort() {
        return port;
    }

    @Override
    public void setUser(String s) {
        user = s;
    }

    @Override
    public void setPassword(String s) {
        password = s;
    }

    @Override
    public void setPort(int p) {
        port = p;
    }

    @Override
    public DefaultDevice getHost() {
        return device;
    }

    @Override
    public void setHost(DefaultDevice host) {
        System.out.println("[ProtocolInterface].CredentialInterface.setHost() host= "+host);
        device = host;
    }
};

boolean useIP = true;

public ProtocolInterface() {
}

public CredentialInterface getCredential() {
    return credential;
}

public ProtocolInterface(String name, DefaultDevice device) {
    this.name = name;
    this.device = device;
}

public DefaultDevice getDevice() {
    return device;
}

public ReturnInterface<N> getReturnInterface() {
    return returnInterface;
}

public void setReturnInterface(ReturnInterface<N> returnInterface) {
    this.returnInterface = returnInterface;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Task getTask() {
    return task;
}

public void setTask(Task task) {
    this.task = task;
}

public Date getScheduledDate() {
    return scheduledDate;
}

public void setScheduledDate(Date scheduledDate) {
    this.scheduledDate = scheduledDate;
}

public abstract Icon getIcon();

public abstract CredentialForm_Interface getCredentialForm();


@Override
public int hashCode() {
    int hash = 7;
    hash = 47 * hash + Objects.hashCode(this.device);
    hash = 47 * hash + Objects.hashCode(this.name);
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final ProtocolInterface<?> other = (ProtocolInterface<?>) obj;
    if (this.useIP != other.useIP) {
        return false;
    }
    if (!Objects.equals(this.name, other.name)) {
        return false;
    }
    if (!Objects.equals(this.device, other.device)) {
        return false;
    }
    if (!Objects.equals(this.credential, other.credential)) {
        return false;
    }
    return true;
}



@Override
public String toString() {
    return name;
}

}

3 个答案:

答案 0 :(得分:0)

是的,您是对的!您参考协议对象创建Runnable实例。该引用可以更改,直到invokeLater执行此任务。因此,您必须复制所需的数据而不是保存引用。

SwingUtilities.invokeLater(new Runnable() {
        final Device device = selectedProtocol.getDevice();
        @Override
        public void run() {  
            System.out.println("[Taskmanager.TaskReturnInterface.actionPerformed.RUN()]selectedProtocol device= " + device);
        }
    });

答案 1 :(得分:0)

尝试一下:

Iterator<TaskCard> i = taskManager.getTaskCards().iterator();

while (i.hasNext()) {
    TaskCard taskCard = i.next();
    taskCard.updateTask();
    ReturnInterface<String> returnInterface = new TaskReturnIterface(taskManager, taskCard);
    Task task = taskCard.getTask();
    final ProtocolInterface selectedProtocol = task.getDevice().getSelectedProtocol();
    selectedProtocol.setTask(task);
    selectedProtocol.setReturnInterface(returnInterface);

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {  
            System.out.println("[Taskmanager.TaskReturnInterface.actionPerformed.RUN()]selectedProtocol device= " + selectedProtocol.getDevice());
        }
    });
}

答案 2 :(得分:0)

如果没有关于TaskCard和其他对象的更多信息,很难说,但是您应该尝试声明一些变量final并尝试打印该对象的哈希码,以检查它是否确实是相同的 instance 或不同的实例在语义上相等:

for (Iterator<TaskCard> i = taskManager.getTaskCards().iterator(); i.hasNext();) {
    TaskCard taskCard = i.next();
    taskCard.updateTask();
    ReturnInterface<String> returnInterface = new TaskReturnIterface(taskManager, taskCard);
    Task task = taskCard.getTask();
    // Mark this as "final" so you can use it as is in any internal anonymous class:
    final ProtocolInterface selectedProtocol = task.getDevice().getSelectedProtocol();
    selectedProtocol.setTask(task);
    selectedProtocol.setReturnInterface(returnInterface);
    System.out.println("[1] selectedProtocol device=" + selectedProtocol.getDevice().hashCode());
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {  
            System.out.println("[2] selectedProtocol device=" + selectedProtocol.getDevice().hashCode());
        }
    });
}

在对象之间似乎存在一些联系,它们可能打印相同的输出或在后端使用相同的对象。特别是这部分:

ProtocolInterface selectedProtocol = task.getDevice().getSelectedProtocol();
selectedProtocol.setTask(task);
selectedProtocol.setReturnInterface(returnInterface);

看起来很奇怪,因为selectedProtocol似乎以某种方式绑定到与任务绑定的设备本身,然后您必须重新设置其任务?

基本上是在做task.getDevice().getSelectedProtocol().setTask(task),看来您应该检查一下……

此外,SwingUtilities.invokeLater()是保留给GUI处理的,因此您可能希望删除它(除非它正在执行GUI ...)。