I'm sorry that my question was not clear. I should have given some code which I'm working with. please refer to the following link:
http://www.arduino.cc/playground/Interfacing/Java
Basically this sample code is different from those classic rxtx examples with event based communication on serial port by declaring "synchronized" keyword on some functions. The only part which i am working on is within the serial event listener:
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
////////////////////////////////////
1) I will parse the incoming data into an integer array
2) this array will be shared with other classes, let me
explain some of them below.....
////////////////////////////////////
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
Function A: an applet doing 2D/3D graphic using the data in this shared array.
Function B: a GUI will output/print some numbers calculated from the shared array to a textarea in a jframe. It can also open up Function A applet with a button. But there are some buttons in the GUI will change some variables in Function A to cause different behavior to process the shared array by Function A.
This is what I want:
As the serial data coming in and parsed into the data array....
1)Function B will immediately print the numbers associated with this data array. Only if the user click the button to show up the Function A applet, Function A will display 2D/3D graphic.
2) When Function A and B are both active, their own variables are subject to change along with the incoming serial data.
I want Function A and B catch the change in the shared data array right away as it is changed in the serialEvent(). I know the most simplest way is to pass the data array as an reference and call each function before the next incoming data become available. But there is the problem mentioned previously by Grodriguez. There might be some lost incoming data while Function A or B is in progress. I did something about MVC in java years ago, and it was using obervables between different classes or GUIs to display temperature. Like when I enter a temperature in the textbox, the other GUI with the slider will change to the input temperature. And as I changed the slider here, it will change the temperature in number in the other GUI. The serial event reminds me of this application here. I am not sure whether it's the best method to apply here or there may be other better and easier ways to do exactly what I want.
Thank you for your patience again.