9 Replies Latest reply: Jan 12, 2008 8:26 AM by 807603 RSS

    how to synchronize an Array List

    706710
      hi Java Community pple,
      how do i synchronized an arraylist in core java???

      If u have an example can you please paste it here.

      Assuming
      list l1 = new ArrayList();

      how do i synchronize the above array list without using a synchronize block...

      can any one help me out with an example.
      Code attached will be highly helpful and appreciated.
        • 1. Re: how to synchronize an Array List
          807603
          The Vector is the "syncronized" version of the ArrayList. Why don't you use that?
          • 2. Re: how to synchronize an Array List
            807603
            If I remember correctly:
            ArrayList<WhateverType> arrayList = new ArrayList<WhateverType>();
            List synchronizedList = java.util.Collections.synchronizedList(arrayList);
            • 3. Re: how to synchronize an Array List
              706710
              Hi PPle,
              can u send me a working code/Example so that i can compile and check it out whether its working or not???

              Bye
              Deepak
              • 4. Re: how to synchronize an Array List
                807603
                The conversion is really easy: from the api of vector there is a contructor:

                Vector(Collection c)

                Constructs a vector containing the elements of the specified collection, in the order they are returned by the collection's iterator.

                list l1 = new ArrayList();
                
                //add all the elemnts that you want
                
                Vector v = new Vector(l1); 
                • 5. Re: how to synchronize an Array List
                  807603
                  yeh, forget the vector, the solution of bryjar is much better (I think)
                  public static void main(String[] args) {
                          List<String> myList = new ArrayList<String>();
                          myList.add("bla bla");
                          
                          List<String> syncronizedList = java.util.Collections.synchronizedList(myList);
                        
                      }
                  But remember that the last one returns a List, not an ArrayList, so you have to use the methods in the List interface.
                  • 6. Re: how to synchronize an Array List
                    807603
                    kinka-dzs wrote:
                    The Vector is the "syncronized" version of the ArrayList. Why don't you use that?
                    Your most recent comments are correct. This statement above is patently wrong. The two are not versions of each other.
                    • 7. Re: how to synchronize an Array List
                      807603
                      Sorry encephalopathic,
                      but could you be a bit more specific than? Now I would like to clear this in my mind. What I think is:
                      "The methods of the Vector class are syncronized, so different threads can not add elemnts in the same moment(for example),
                      while a Sycronized List made as with the code above is syncronized (has a lock on the object)."

                      is that correct? If not please enlight me. I think the api of vector than is a bit misleading.
                      • 8. Re: how to synchronize an Array List
                        807603
                        Yes Vector is synchronized, but as noted above, you can get synchronized List implementations via a synchronizedList call. From what I've read, Sun discourages the use of Vector, that it may eventually be on its way out, and that the only reason it hasn't been deprecated yet is due to all the legacy code that still uses it (i.e., JList and JTable to name two). You should avoid using Vector unless you have a very definite reason to do so. I am no expert here, but there are others who are, and if you search long enough, you can find one of many 200+ message threads that debate vector vs. arraylist. Much luck!
                        • 9. Re: how to synchronize an Array List
                          807603
                          Yes, there's always the thread that wont die. You'll also find international conflict among other things here:
                          http://forum.java.sun.com/thread.jspa?threadID=572344