DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more

ListenerList - a Better Way to Manage Your Event Listeners

Prakash  user avatar by
Prakash
·
Dec. 18, 08 · Interview
Like (0)
Save
Tweet
Share
14.60K Views

Join the DZone community and get the full member experience.

Join For Free

You have published a listener interface and have a place where your clients register their listeners. There you typically code like this:

List<SomeEventListener> listeners = new ArrayList<SomeEventListener>(); 

public void addSomeEventListener(SomeEventListener listener) { 
    listeners.add(listener); 
} 

public void removeSomeEventListener(SomeEventListener listener) { 
    listeners.remove(listener); 
} 

public void fireSomeEvent(SomeEvent event) { 
    for (SomeEventListener listener : listeners) { 
        listener.eventOccured(event); 
    } 
}

The problem with this approach is two fold:

  1. Memory usage: The capacity of the ArrayList is always >= its size. So if you just have 200 listeners, there is a good chance that the memory might have been allocated for 300 elements
  2. Thread safety: Murphy's law will prove itself, and multi threaded bugs can be a pain in your back to debug. You have to properly sync all the write access and provide faster read only access for event firing operations.

To handle these two issues, Eclipse gives you a nice API: ListenerList class. Its meant for storing these kind of listeners, where any write modification to the underlying array (add, remove, clear) is synchronized and give access to the underlying array for faster read access. Memory is allocated only for the number of listeners registered.

The same above example can be rewritten as:

ListenerList listeners = new ListenerList(); 

public void addSomeEventListener(SomeEventListener listener) { 
    listeners.add(listener); 
} 

public void removeSomeEventListener(SomeEventListener listener) { 
    listeners.remove(listener); 
} 

public void fireSomeEvent(SomeEvent event) { 
    Object[] listenersArray = listeners.getListeners(); 
    for (int i = 0; i < listenersArray.length; i++) { 
        ((SomeEventListener)listenersArray[i]).eventOccured(event); 
    } 
}

 

ListenerList provides two ways of duplicate checking of the listeners: Equality and Identity. It can be specified in the constructor. Since the core Eclipse platform is in Java 1.4, you still can't use generics and foreach, but that shouldn't be a hindrance.

Question: Now that I do have a direct access to the underlying array, what if i manipulate it like this:

Object[] listenersArray = listeners.getListeners(); 
listenersArray[0] = null; 
listenersArray[1] = someEventListener5;

 

Well, the same thing happens when you shoot yourself in the foot :-)

 

From http://blog.eclipse-tips.com/
Event

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What’s New in Flutter 3.7?
  • 5 Common Firewall Misconfigurations and How to Address Them
  • Spring Cloud
  • Building a RESTful API With AWS Lambda and Express

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: