Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 479555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:48:15+00:00 2026-05-13T00:48:15+00:00

I have created a panel on which a set of objects is drawn. Each

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:48:15+00:00Added an answer on May 13, 2026 at 12:48 am

    As for Noel comment:

    A concern that this technique might trip on is if a particular listener implementation returns a notification early, and executes work “out-of-band” on a separate thread. In that case, the end of the notification call does not actually mark the end of the listener’s execution. If this is an actual concern (i.e, you’re aware of and concerned with other user-defined, elaborate listeners that do this sort of thing, you might want to refine and revisit the problem.

    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:

    pseudocode:

    Listener implements MouseListener 
        +mouseClicked( event: MouseEvent ) 
            SwingUtilities.invokeLater( // returns immediately 
                  someTask() 
            )
    
        -someTask()
            // perform some long task.
    

    If you have your listeners as

     addListener( new Listener() )
     addListener( new Listener() )
     addListener( new Listener() )
     addListener( new 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.

    So, what you do?

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.