Results 1 to 2 of 2
  1. #1
    siteuser is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default CopyOnWriteArraySet Vs just toArray()

    Hi Experts,

    I would like to know your opinion on which one is better or preferred to invoke callback methods see below example.

    Also is CopyOnWriteArraySet better than toArray(T[] ts) for this example, I feel toArray(T[] ts) is better.

    What's your opinion?

    Java Code:
    interface SomeListener {
        void callBack();
    }
    //////////////
    class AbstractService {
        protected List<SomeListener> listenerList;
        abstract init();
        addSomeListener(SomeListener listener) {
            listenerList.add(listener);
        }
        removeSomeListener(....) {...}
        abstract fireCallBack();
    }
    ///////////////
    class ToArrayService extends AbstractService {
        init() {
            listenerList = new ArrayList<SomeListener>();
        }
    
        fireCallBack() {
             SomeListener[] listeners = new SomeListener[listenerList.size()];
             listeners = (SomeListener[])listenerList.toArray(listeners);
            for(SomeListener listener : listeners) {
                listener.callBack();
            }
        }
    }
    /////////////
    class CopyOnWriteArraySetService extends AbstractService {
        init() {
            listenerList = new CopyOnWriteArraySet<SomeListener>();
        }
    
        fireCallBack() {
            for(SomeListenerlistener : listenerList) {
                listener.callBack();
            }
        }
    }
    thanks in advance
    siteuser

  2. #2
    siteuser is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: CopyOnWriteArraySet Vs just toArray()

    I found my answer after reading the following:
    http://book.javanb.com/java-threads-...-8-SECT-2.html

    cheers

Similar Threads

  1. efficiency of arraylist toArray()
    By xael in forum Java Gaming
    Replies: 0
    Last Post: 02-27-2011, 06:43 PM
  2. Replies: 2
    Last Post: 04-16-2008, 01:35 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •