Best way to pass objects between classes in different threads?

I have a class "A" that is "runnable" and I'm making new objects from Java markers. The MainGUI thread tries to access these instances using get (), which is already in class "A". The instances that I created in class A, I made them static so that they are available forever, but the problem is, when I get a new full instance with different properties, I need to compare the new instance with the previous data and save the new one.

Is there a better way or design for this problem?

How can I get class A instances created at runtime without statics?

Code example:

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

}
+5
source share
2 answers
+3

"A" . A . -

A aVariable;
Thread t = new Thread(new MyThread(aVariable));
t.start();
t.join();
populateGUI(aVariable);
0

All Articles