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 4597280
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:14:05+00:00 2026-05-21T23:14:05+00:00

I have an object in my design that has listeners. These listeners get fired

  • 0

I have an object in my design that has listeners. These listeners get fired on a certain event that can occur as many as a hundred times per second.

I was doing something like this:

private void notifyListeners(ObjectEvent o) {
    synchronized (this.listeners) {
        for (ObjectListener l: this.listeners)
            l.eventFired(o);
    }
}

The problem here is that someone can implement an eventFired method which then turns around and waits for a synchronizeto an object that’s being held by a different thread which is trying to add or remove a listener and waiting on a synchronized(this.listeners) line.

So, I modified the notifyListeners method thusly:

private ObjectListener[] getObjectListeners() {
    synchronized (this.listeners) {
        return this.listeners.toArray(new ObjectListener[this.listeners.size()]);
    }
}

private void notifyListeners(ObjectEvent o) {
    ObjectListener[] listeners = this.getObjectListeners();
    for (ObjectListener l: listeners)
        l.eventFired(o);
}

I’m worried about the impact of creating this array every time the object is fired, and the impact this will have on the memory usage of the application.

So I’m here to ask if there’s a better way. I may have just found it, though. It would be easy enough to create the ObjectListener[] every time I add or remove a listener and then just iterate through that with no synchronization every time the event fires. I’m going to make that change now, and then see if there’s a better idea offered up here.

  • 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-21T23:14:06+00:00Added an answer on May 21, 2026 at 11:14 pm

    Use CopyOnWriteArrayList instead. The observation is that listener lists are typically inspected a lot more often than they are modified.

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

Sidebar

Related Questions

I have two tables: object that has object_id column and avalues that have object_id
I have an object of class Level that has several subsystems (child objects) it
I have an object can be compose by components but each components has a
I have an object, based on the Singleton design, that I use for user
I often design system where I have a class in my system that has
Can I get a dummy logger from slf4j? (Think the null object design pattern.)
I have an object User that has attributes whose accessability is declared as protected
I currently have a routes object that has several regular pages, and would like
I have purchase Object-Oriented Analysis and Design with Applications , at page 64 paragraph
We have lot of object with this kind of design : Interface and several

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.