question about listener..

hello,

I have built a class (class A )that implemented an interface that has a listener method called

public void onDataIncoming(String str);

I coded the onDataIncoming method as follow :

Note : String incoming, a global variable

publicvoid onDataIncoming(String str)

{

incoming = incoming + str;

}

so, basically I append the data i received into the incoming variable. And the rate of incoming data is very fast, and keeps on coming in...

Then I have another class (class B )that runs an independent thread. I let this thread runs continuosly, and after every 100 miliseconds, it will call a method in class A. This method is as follows:

public String processIncoming()

{

String temp = incoming;

incoming ="";

return temp;

}

Due to the early design of my program, i have to mearge 2 different functions in my programs. Its difficult for me to make too much changes to my code.

So, my question is, base on what I have implemented, will the codes gives me problem in the long run. It runs well so far though...

thanks..

Message was edited by:

a35176

[1447 byte] By [a35176a] at [2007-11-26 12:45:14]
# 1
The short form answer is there's no way to tell. It may or it may not depending on way too many factors to predict reliably. Looking at what you've got it's not the way I would have done it but that doesn't matter. If it's working go with it until you have a reason to change
puckstopper31a at 2007-7-7 16:23:45 > top of Java-index,Java Essentials,Java Programming...
# 2

actually, my question is that...

when the listener is trying to append new data into the 'incoming', but in the same time, the other method is being called, and it tries to clear the string in 'incoming'.

What will happen if both of that methios is being called at the same time?

a35176a at 2007-7-7 16:23:45 > top of Java-index,Java Essentials,Java Programming...
# 3

> when the listener is trying to append new data into

> the 'incoming', but in the same time, the other

> method is being called, and it tries to clear the

> string in 'incoming'.

>

> What will happen if both of that methios is being

> called at the same time?

You need to use "synchronized" code. Then only one block of code can access 'incoming' at a time.

doremifasollatidoa at 2007-7-7 16:23:45 > top of Java-index,Java Essentials,Java Programming...
# 4
make this method synchronizedsynchronized public String processIncoming(){String temp = incoming;incoming = "";return temp;}
SUSHANT_Ja at 2007-7-7 16:23:45 > top of Java-index,Java Essentials,Java Programming...
# 5
Synchronizing the one method only is of no use. You have to synchronize both methods or a dedicated object.Message was edited by: stefan.schulzCorrected wording.
stefan.schulza at 2007-7-7 16:23:45 > top of Java-index,Java Essentials,Java Programming...