synchronization and threads.
Hi,
Here is my problem.
I have a vector of threads, called LISTENER.
These threads are identified uniquely by a PHONENUMBER.
Once I receive some data (text + phoneNumber), I notify all the threads registered in LISTENER that some data is available. Each thread knows if it is for it by comparing PHONENUMBER and phoneNumber in the data.
If the data is for none of elements in LISTENER, I create a new thread and register it in LISTENER.
The problem is when I do that, in my method notify, LISTENER is modified instantly. And then, this new thread receives also the information and I don't want that. How can I stop the access to LISTENER during this for loop (cf code below)? I synchronized the methods, it didn't work. I synchronized inside each method with "synchronized(this.LISTENER)" and it didn't work either. Does anyone know how to do?
Here is a part of the code
public class Testeur {
Vector listener = new Vector(0);
public synchronized void notifyReceiveData(String Text, String phoneNumber){
for (int i = 0; i < this.listener.size(); i++) {
this.listener.elementAt(i)).receiveData(Text,phoneNumber);
}
}
public synchronized void addSMSListener(SMSListener listener){
this.listener.addElement(listener);
}
}
public class Testeur2 {
public void createThread(String Text, String phoneNumber){
Thread sm = new Thread();
sm.setIdentifier(phoneNumber);
sm.start();
t.addSMSListener(sm);
}
public receiveData(Text, phoneNumber){
if (phoneNumber unknown){
createThread(Text, phoneNumber);
}
}
}
[1705 byte] By [
mogabure] at [2007-9-26 1:48:36]

i am not sure i understand, but is this what you want:
public synchronized void notifyReceiveData(String Text, String phoneNumber){
int size = listener.size();
for (int i = 0; i < size; i++) {
this.listener.elementAt(i)).receiveData(Text,phoneNumber);
}
}
this way any element added to listener during the call to receiveData will not be invoked while you are in this loop.
hth
partha
I thought about that but it doesn't work. First, it adds the element even if you are in the for loop. Second, if you remove an element in LISTENER during the loop, it happens an ArrayIndexOutOfBoundsException.
The problem in the for loop is that it gets each element at a time and not the whole vector for a time. So, each time the getElement is done, the vector is free for other operation: add, remove, ....
OUCH. each thread is callign receive data. so if you start out with 2 phone number threads running. then a 3rd unidentified number comes, BOTH threads will create new threads to handle the new number. thats why you are seeing the "instant" call on the new thread. its not really instant.
You need a master thread that will create the new threads. You cant let each thread be responsible for deciding if a new thread is needed, since each will think the answer is yes. and you threads will be created like this
1 #1
2 #1 & #2
4 #1 & #2 & #3 & #3
8 #1 & #2 & #3 & #3 & #4& #4& #4& #4
Try this
public void notifyReceiveData(String Text, String phoneNumber)
{
boolean handled = false;
for (int i = 0; i < this.listener.size() && handled == false; i++) {
handled = this.listener.elementAt(i)).receiveData(Text,phoneNumber);
}
if(handled == false)
createThread(String Text, String phoneNumber);
}
public boolean receiveData(Text, phoneNumber){
if (phoneNumber unknown){
return false;
else
doIT();
return true;
}
}
}
This is still poor code and does not make use of the power of threads. they might as well be simple objects.