5

我有一个“可运行”的“A”类,我用 Java 解组器制作新对象。MainGUI 线程尝试通过已在类“A”中的 get() 访问这些实例。我在 A 类创建的实例,我将它们设为静态,以便它们永远可用,但是当我得到一个具有不同属性的新完整实例时,我必须将新实例与前一个数据进行比较并保持新的那一个。

有没有更好的方法或设计来解决这个问题?

如何获取在运行时创建的“A”类实例而不使它们成为静态的?

示例代码:

    public class SOAPMessagesFactory {

     private static GetCameraImageResponse                getCameraImageResponse;

// process here the msgs in another thread, not shown here in that snipped
     if (messageTag.equalsIgnoreCase("GetCameraImageResponse")) {
                try {
                    JAXBElement<GetCameraImageResponse> cameraImageResponse = unmarshaller.unmarshal(SoapBodyReader, GetCameraImageResponse.class);
                    getCameraImageResponse = cameraImageResponse.getValue();

                } catch (Throwable ex) {
                    ex.printStackTrace();
                }

            }

public GetCameraImageResponse getCameraImageResponse() {

    if (getCameraImageResponse != null) {
        return getCameraImageResponse;
    } else {
        return null;
    }

}
     // in main gui

 public void UpdateGUI() {

        GetCameraImageResponse cameraImageResponse = messageFactory.getCameraImageResponse();

}
4

2 回答 2

3

尝试生产者-消费者模式。

于 2013-04-15T11:24:30.117 回答
0

您可以在 main 中声明“A”类型的引用并将其传递给另一个线程。在另一个线程中执行操作并将 A 的新实例分配给引用。像这样的东西

A aVariable;
Thread t = new Thread(new MyThread(aVariable));
t.start();
t.join();
populateGUI(aVariable);
于 2013-04-15T11:22:41.217 回答