简体   繁体   English

添加第一个元素或删除最后一个元素时触发事件的设置

[英]Set that fires event when first element is added, or last one is removed

I've got an API that allows users to listen to network events.我有一个允许用户监听网络事件的 API。 I want to initiate a network connection when the first event listener is added, and disconnect when the last listener is removed.我想在添加第一个事件侦听器时启动网络连接,并在删除最后一个侦听器时断开连接。

Properties:属性:

  • The set (of listeners) is read much more often than written to.一组(监听器)的读取频率远高于写入频率。
  • I need to be able to modify and read the set concurrently from multiple threads.我需要能够从多个线程同时修改和读取该集合。
  • I need to fire an event when the first element is added (connect to the network), and when the last element is removed (disconnect from the network).我需要在添加第一个元素(连接到网络)和删除最后一个元素(与网络断开连接)时触发一个事件。

What I've looked at so far:到目前为止我看过的内容:

  • CopyOnWriteArraySet : thread-safe set that is optimized for reading more frequently than writing. CopyOnWriteArraySet :线程安全集,针对读取比写入进行了优化。 There is no mechanism for running an operation when the first element is added or last one is removed.添加第一个元素或删除最后一个元素时,没有运行操作的机制。
  • Phaser : fires an event when the last element is removed, but not when the first one is added. Phaser :当最后一个元素被移除时触发一个事件,但在第一个元素被添加时不会触发。 I guess I could use two Phasers to achieve this behavior but it seems wasteful.我想我可以使用两个Phasers来实现这种行为,但这似乎很浪费。

Maybe ReadWriteLock is good for you?也许ReadWriteLock对你有好处?

A ReadWriteLock maintains a pair of associated locks, one for read-only operations and one for writing. ReadWriteLock 维护一对关联的锁,一个用于只读操作,一个用于写入。 The read lock may be held simultaneously by multiple reader threads, so long as there are no writers.只要没有写者,读锁就可以被多个读者线程同时持有。 The write lock is exclusive.写锁是独占的。

Simple example (i use hashmap instead set):简单示例(我使用 hashmap 代替 set):

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class LockWrapper {

private final ReadWriteLock readWriteLock;
private final Map<Integer, Object> map;
private final EventSender sender;

public LockWrapper(EventSender eventSender) {
    map = new HashMap<>();
    readWriteLock = new ReentrantReadWriteLock();
    sender = eventSender;
}

public void add(Object o) {
    readWriteLock.writeLock().lock();
    try {
       if (map.isEmpty()) {
          sender.send("First add");
       }
       map.put(o.hashCode(), o); // for example using hashcode for key
    }
    finally {
       readWriteLock.writeLock().unlock();
    }
}

public void remove(Object o) {
    readWriteLock.writeLock().lock();
    try {
      map.remove(o.hashCode());

      if (map.isEmpty()) {
         sender.send("remove last");
      }
    }
    finally {
      readWriteLock.writeLock().unlock();
    }  
}

public Object read(int key) {
    readWriteLock.readLock().lock();
    try {
      Object object = map.get(key);
    } 
    finally {
      readWriteLock.readLock().unlock();
    }  
    return object;
}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查2组并获取所有已删除/添加的表单/到第一组元素 - Check 2 sets and get all removed/added form/to first set elements 添加/删除项目时,Richfaces pickList ajax事件 - Richfaces pickList ajax event when item has been added/removed 我如何创建每次将新元素添加到ArrayList时都会触发的事件 - How do I create a event that fires up every time a new element is added to ArrayList java add函数,添加后全部成为最后一组数据 - java add function,all become one last set of data when added 我正在尝试获取集合中的最后一个date元素,但是它一直返回第一个? - I am trying to get the last date element in a set, however It keeps returning the first one? 删除侦听器后,可以再在该侦听器上调用一次事件吗? - When a listener is removed, is it okay that the event be called on that listener one more time? 活动运行时是否有触发事件? - Is there an event to fires when activity run? 将值从一个Arraylist复制到另一个时,所有值均设置为在第一个Arraylist中输入的最后一个值 - When copying the values from one Arraylist to another, all values get set as the last value entered in the first Arraylist 如何根据 OptaPlanner 中添加的第一个元素设置参数? - How to set a parameter based on the first element added in OptaPlanner? 有没有一种方法可以测试何时从弱集合中删除元素? - Is there a way to test when an element is removed from a weak set?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM