public void addListener(String eventType, EventListener eventListener) { this.listenerRWLock.writeLock().lock(); // 加锁(写) // Hasor.assertIsNotNull(eventListener, "add EventListener object is null."); EventListener[] eventListenerArray = this.listenerMap.get(eventType); if (eventListenerArray == null) { eventListenerArray = new EventListener[] {eventListener}; this.listenerMap.put(eventType, eventListenerArray); } else { if (ArrayUtils.contains(eventListenerArray, eventListener) == false) { eventListenerArray = (EventListener[]) ArrayUtils.add(eventListenerArray, eventListener); this.listenerMap.put(eventType, eventListenerArray); } } // this.listenerRWLock.writeLock().unlock(); // 解锁(写) }
// public void removeAllEventListener(String eventType) { // this.listenerRWLock.writeLock().lock();//加锁(写) // this.listenerMap.remove(eventType); // this.listenerRWLock.writeLock().unlock();//解锁(写) // } public void removeListener(String eventType, EventListener eventListener) { this.listenerRWLock.writeLock().lock(); // 加锁(写) // Hasor.assertIsNotNull(eventType, "remove eventType is null."); Hasor.assertIsNotNull(eventListener, "remove EventListener object is null."); EventListener[] eventListenerArray = this.listenerMap.get(eventType); if (!ArrayUtils.isEmpty(eventListenerArray)) { int index = ArrayUtils.indexOf(eventListenerArray, eventListener); eventListenerArray = (EventListener[]) ((index == ArrayUtils.INDEX_NOT_FOUND) ? eventListenerArray : ArrayUtils.remove(eventListenerArray, index)); this.listenerMap.put(eventType, eventListenerArray); } // this.listenerRWLock.writeLock().unlock(); // 解锁(写) }