简体   繁体   中英

While Loop variable assignment to Runnable

Why does each Runnable print the same device when each one is supposed to be assigned a different device?

It seems like each Runnable is using the last device assigned from the while loop. How can I ensure that each Runnable is assigned one device from the loop?

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

}

Below is the Protocol Interface code that wsa requested.

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;
}

}

Yes, you're right! You create Runnable instance with reference to the protocol object. This reference could be changed until invokeLater runt this task. So you have to copy required data instead of save reference.

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

Try this:

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

It's difficult to say without more information on TaskCard and other objects but you should try to declare some variable final and try to print the hash code for the object, to check if it really is the same instance or a different instances semantically equal:

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

It would seem there are some ties between objects that might either print the same output or use the same object in the backend. Especially this part:

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

looks strange as the selectedProtocol seems somehow tied to a device itself tied to a task, then you have to set its task again?

It basically is doing task.getDevice().getSelectedProtocol().setTask(task) which looks like there is some kind of loophole you should check...

Also, the SwingUtilities.invokeLater() is kind of reserved for GUI processing so you might want to remove that (unless it is doing GUI...).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM