wait for a method to be called

Wich is the best way to implement the "// wait till codeFill() is called from some other object" part of this algorithm?:

public class SomeObject {

public void someMethod() {

// exec code A

codaA();

// exec code B

codeB();

// exec code C

codeC();

// wait till codeFill() is called from some other object...

// process the filled data

codeProcessFill();

}

}

[443 byte] By [Arathron] at [2007-9-30 23:25:59]
# 1
Why don't you just put the code that depends on codeFill() in codeFill() or a method that codeFill calls?
dubwai at 2007-7-7 14:01:49 > top of Java-index,Other Topics,Algorithms...
# 2

That's preciselly the matter. Think of it as if someMethod() can be called at any moment from another object but it needs codeFill() to be called to complete, as it needs the information obtained externally by codeFill() and return with some result after processCodeFill().

Maybe codeFill is an event from a user, an accept button for example, the method must wait till some user accepts before completion.

So i need a way to 'hang' on codeFill and not continue till this method is called from some other object.

(In the example codeFill could be acceptButtonPressed event, or a comboValueChanged and and processCodeFill() would process it)

Thanxs for the reply.

Arathron at 2007-7-7 14:01:49 > top of Java-index,Other Topics,Algorithms...
# 3
I'm not sure I understand the problem. Is someMethod() going to trigger a request for information from the user? Otherwise, do you want the method to block indefinitely?
dubwai at 2007-7-7 14:01:49 > top of Java-index,Other Topics,Algorithms...
# 4

public class SomeObject {

public void someMethod() {

// exec code A

codaA();

// exec code B

codeB();

// exec code C

codeC();

// wait till codeFill() is called from some other ther object...

wait();

// process the filled data

codeProcessFill();

}

void codeFill(){

notify();

}

}

There should be enough rope here for you to hang yourself with now ;)

_dnoyeB at 2007-7-7 14:01:49 > top of Java-index,Other Topics,Algorithms...
# 5

Thanks a lot!

I didn't realice before it was some kind of the producer-consumer pattern, isn't it?

codeFill() produces and codeProcessFill() consumes. I just needed to put the thread to sleep till the producer awakes it.

That solved the problem, new comments or suggestions are welcome.

And again, thanks a lot for the replys!

Arathron at 2007-7-7 14:01:49 > top of Java-index,Other Topics,Algorithms...