user1217609
user1217609

Reputation: 253

executing code too fast..need to slow down but how?

from this code below

  if(!validate_containsNum && !validate_isNumber && !validate_isBlankField){
                a.sendMsgToSlave("DATA:TAKE:PEOPLEs:"+pName_sub);
                appendToGlobalDataLog("SEND : DATA:TAKE:PEOPLEs:"+pID+":"+p_sub);
                String pID = pID_Manage.getText();  
                a.addtoArrayList("DATA:TAKE:PEOPLEs:"+pID+":"+pName_sub);
                at.sendToServer("DATA:TAKE:PEOPLEs:"+pID+":"+pName_sub);



        }

from the code above i am having a problem where this line

      a.sendMsgToSlave("DATA:TAKE:PEOPLEs:"+pName_sub);

is actually calling my client to fetch data from peoples TABLE in order to get pID in return which will be return to a JTEXTFIELD where the next line should be executed

     String pID = pID_Manage.getText();  

but then as for the next line job is to add certain String into an arraylist which has the combination of pID and pName_Sub but then when executed it i found out my arraylist did not have the pID..so instead of the real result should be something like this

     DATA:TAKE:PEOPLEs:1:ALBERT  (In my arraylist)

it shows like this

     DATA:TAKE:PEOPLEs::ALBERT (the id is missing)

i need to resubmit again the 2nd time only can get the ID...it seems like the execution of the code is too fast and the storing of ID in return to the JTEXTFIELD is slow..any way to slow it down?

Upvotes: 1

Views: 2017

Answers (4)

Luke
Luke

Reputation: 5076

I'm not sure if this will work, but with things like trying to prevent rainbow tables in hashes and other security problems, doing something like

for(int i = 0; i < 100; i++) {
}

might do the trick. But again I've never tested this, it's just a suggestion

Upvotes: -1

Marko Topolnik
Marko Topolnik

Reputation: 200158

Your sendMsgToSlave starts an asynchronous chain of events that results in the text field being updated at some later point. You should modify your architecture so that the code that depends on the ID field being set runs in reaction to that event actually happening. As others have mentioned, you can make sendMsgToSlave synchronous, but judging from its name, I don't think that's a really good idea. What you could do is attach a change listener to the ID field that will do the rest of the task. I'll approximate as I don't know the exact listener you're going to need:

pID_Manager.addChangeListener(new ChangeListener() { 
  public void textChanged(ChangeEvent e) {
    pID_manager.removeChangeListener(this);
    String pID = pID_Manage.getText();  
    a.addtoArrayList("DATA:TAKE:PEOPLEs:"+pID+":"+pName_sub);
    at.sendToServer("DATA:TAKE:PEOPLEs:"+pID+":"+pName_sub);
}};

So the basic idea is that the listener reacts to the appropriate event and that it immediately deregisters itself upon firing.

Upvotes: 1

GingerHead
GingerHead

Reputation: 8230

the following is your sollution:
put changelistener on the jtextfield pID_Manage
and call your array populating method in it
so that the array will only be populated when the id is recieved and assigned to the jtextfield.

Upvotes: 0

Attila
Attila

Reputation: 28762

The quick and dirty solution is to call Thread.Sleep, but the better solution is to modify the code in a.sendMsgToSlave() to return only after the id is set. That way you know for sure that when the execution gets to the statement after sendMsgToSlave() you will definitely have the ID ready every time.

Upvotes: 1

Related Questions