Java – the best way to pass objects between classes in different threads?
•
Java
I have a "runnable" class "a" and I use Java unmarshallers to create new objects The maingui thread attempts to access these instances through get() already in class "a" For the instances I create in class A, I set them to be static so that they are always available. However, when I get a new complete instance with different properties, I must compare the new instance with the previous data and keep the new one unchanged
Is there a better way or design to solve this problem?
How do I get an instance of class "a" created at run time without making it static?
Example code:
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();
}
Solution
Try producer consumer mode
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
