简体   繁体   English

等待侦听器完成

[英]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 . 我也有一个静态方法foo() ,该方法访问该数组,然后执行其他操作

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. foo()知道何时触发监听器。

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. foo()无法知道这种情况是否会发生或发生了多少次。

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? 因此,如何强制foo()等待侦听器执行,然后,如果侦听器尚未触发链式反应,则从中断处继续执行,否则等待任何触发的侦听器完成执行?

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 . 由于这是概念性的,很难确定确切的答案,因此我建议阅读有关LocksConditions http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Condition .html These may provide the capabiliites you're looking for. 这些可能会为您提供所需的功能。

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. foo()可以向您的“侦听器”更改状态的任何对象添加一个PropertyChangeListener,我们将其称为模型。
  • 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.. 然后使用模型,通过在PropertyChangeSupport上触发状态已更改的通知方法来通知foo完成任务后。
  • Then foo could remove its listener after it has completed its action.. 然后foo可以在完成操作后删除其侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM