执行所有鼠标事件侦听器后调用代码 [英] Invoke a code after all mouse event listeners are executed

查看:93
本文介绍了执行所有鼠标事件侦听器后调用代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个面板,在该面板上绘制了一组对象.每个对象都作为鼠标事件侦听器添加到面板.据我所知,事件发生后,将通知侦听器,并且代码可能(或正在执行)在多个线程中执行.是否可以附加一个自定义代码,该代码将在所有侦听器执行完代码后 后执行?

I have created a panel on which a set of objects is drawn. Each of the object is added as a mouse event listener to the panel. As I know, after an event occurs the listeners are notified and the code might be (or is?) executed in several threads. Is it possible to attach a custom code that will be executed after all listeners finish executing their code?

推荐答案

关于Noel的评论:

如果特定的侦听器实现提前返回通知,并在单独的线程上带外"执行工作,则该技术可能会触发的问题.在这种情况下,通知调用的结束实际上并不标记侦听器的执行结束.如果这是一个实际问题(例如,您了解并关注执行此类操作的其他用户定义的精心设计的侦听器,则可能需要细化并重新研究该问题.

使用SwingUtilities.invokeLater(),您可以确保在通知所有侦听器之后,代码已执行.但是这些侦听器之一可能在单独的线程中执行其工作.

Using the SwingUtilities.invokeLater() you are sure the code is executed after all the listener were notified. But one of these listeners may perform it's job in a separated thread.

如果您需要在所有侦听器都收到通知但他们完成工作之后执行代码,则可以执行以下操作:

If you need to execute your code after all the listeners not only were notified but they finished their job you could do something like this:

伪代码:

Listener implements MouseListener 
    +mouseClicked( event: MouseEvent ) 
        SwingUtilities.invokeLater( // returns immediately 
              someTask() 
        )

    -someTask()
        // perform some long task.

如果您的听众为

 addListener( new Listener() )
 addListener( new Listener() )
 addListener( new Listener() )
 addListener( new ExecuteAtTheEnd() ) 

如果所有(或某些)侦听器在不同的线程中执行其工作.您的ExecuteAtTheEnd可能正在通知的末尾执行它的代码,而不是在侦听器代码执行结束时执行.

If all ( or some ) of your listeners perform their job in a different thread. Your ExecuteAtTheEnd may be executing it's code at the end of the notification, but not at the end of the execution of the listeners code.

那么,你做什么?

您必须同步使用某些锁定标志并执行代码,直到不使用该标志为止.

You have to synchronize the usage of some lock flag and execute your code until that flag is not used.

它可能是一个计数器,它在侦听器正在执行时增加,而在不执行时递减:

It may be a counter that is incremented when the listener is executing and decremented when is not:

 Listener implements MouseListener 
    +mouseClicked( event: MouseEvent )
        lockFlagCount++ 
        SwingUtilities.invokeLater( 
              someTask() 
        )

    -someTask()
        // perform some long task.
        lockFlag--

然后您继续操作,直到此标志为假:

And then you just proceed until this flag is false:

  ExecuteAtTheEndListener implements MouseListener 

       + mouseClicked( event: MouseEvent ) 
        SwingUtilities.invokeLater( 
              executeAtTheEnd() 
        )
       - executeAtTheEnd() 
            while( logFlagCount > 0 ) 
                wait()

             if( logFlagCount == 0 ) 
                 // continue with the task.                  

当然,将其放入伪代码中要复杂得多,但是它应该可以工作,如果您处于这种情况.

Of course it is much more complicated as I put it in the pseudo-code, but it should work, if you are in this kind of circumstance.

这篇关于执行所有鼠标事件侦听器后调用代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆