Karlovsky120
Karlovsky120

Reputation: 6362

Wait for listener completion

I will be talking purely conceptual here.

I have a class which has a listener attached to it which does something. I have a whole array of the objects that are constructed from that class

I also have a static method foo() which access that array and then does something else.

Doing something else triggers the listener and if I don't pause something else until something is done, code doesn't work. foo() knows when the listener is triggered.

Also, listener has a capability to directily invoke a listener on another object that was constructed out of the same class. foo() can't know if this will happen or not, or how many times.

So how can I force foo() to wait for listener to execute, and then, if the listener hasn't triggered a chain reaction, continue where it left off, or otherwise wait for any triggered listener to finish executing?

Upvotes: 1

Views: 4410

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You may consider using additional listeners:

  • foo() could add a PropertyChangeListener to whatever your "listener" is changing the state of, let's call it the model.
  • then have the model, notify foo when it has completed its task by firing a notification method on the PropertyChangeSupport that its state has changed..
  • Then foo could remove its listener after it has completed its action..

Upvotes: 1

Jeff Storey
Jeff Storey

Reputation: 57202

Since this is conceptual and it's hard to nail down an exact answer, I'd recommend reading about Locks and Conditions http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Condition.html. These may provide the capabiliites you're looking for.

Upvotes: 2

Related Questions