All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.commons.util.concurrent.BaseNotifyingFuture Maven / Gradle / Ivy

There is a newer version: 9.1.7.Final
Show newest version
package org.infinispan.commons.util.concurrent;

import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author Mircea Markus
 * @since 5.2
 */
public abstract class BaseNotifyingFuture implements NotifyingFuture {

   final Set> listeners = new CopyOnWriteArraySet>();
   volatile boolean callCompleted = false;
   final ReadWriteLock listenerLock = new ReentrantReadWriteLock();


   public final NotifyingFuture attachListener(FutureListener objectFutureListener) {
      listenerLock.readLock().lock();
      try {
         if (!callCompleted) listeners.add(objectFutureListener);
         if (callCompleted) objectFutureListener.futureDone(this);
         return this;
      } finally {
         listenerLock.readLock().unlock();
      }
   }

   protected void fireListeners() {
      listenerLock.writeLock().lock();
      try {
         if (callCompleted) {
            throw new IllegalStateException("Already fired listeners.");
         }
         callCompleted = true;
         for (FutureListener l : listeners) l.futureDone(this);
      } finally {
         listenerLock.writeLock().unlock();
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy