Home java CopyonwritearRayList VS ArrayList

CopyonwritearRayList VS ArrayList

Author

Date

Category

Learning the java.util.concurrent package.
I reread a bunch of articles, but I can’t understand the CopyonwritearRayList thread safety principle. More precisely, the principle understood that the duplicate collection is created, and the work occurs with it. But in practice I can not implement it.
A worthy example did not find. Here is this article In the example, it is clear only that when using CopyonwriteArRayList, you can not use iterator Delete item.
The most important question. In what cases we can use CopyonwriteArrayList’s thread security instead of synchronization. I will be grateful for any example.


Answer 1, Authority 100%

ArrayList Together with synchronization in some high-loaded application will work more slowly than CopyonwriteArRayList, since the usual arraylist will be blocked by any entire flow and will be unavailable for other streams. At the same time, CopyonwriteArRayList does not require blocking streams, as each thread will work with its copy of the sheet. When using COPYONWRITEARRAYLIST, it is possible to delete an item when crossing iteter. But this will not affect the residue, in which removal is removed, if you make an output to the screen and delete an item, you will receive an output to the entire list of the entire list. Here is Here is a fairly clear example of work CopyonwriteArRayList.

import java.util.list;
Import java.util.iterator;
Import java.util.arraylist;
Import java.util.concurrent.copyonwritearRayList;
Public Class CopyonwritearRayListexample
{
  List & lt; String & GT; List;
  Public CopyonwritearRayLisTexample ()
  {
    List & lt; String & GT; LST = New ArrayList & LT; String & GT; ();
    lst.add ("java");
    lst.add ("j2ee");
    lst.add ("j2se");
    lst.add ("Collection");
    lst.add ("concurrent");
    List = New CopyonwritearRayList & LT; String & GT; (LST);
    System.out.PrintLN ("Cool with Change");
    PrintCollection (True);
    System.out.printLN ("\ NcCl without change");
    PrintCollection (False);
  }
  Private Void PrintCollection (Boolean Change)
  {
    ITERATOR & LT; String & GT; ITERATOR = LIST.ITERATOR ();
    While (ITERATOR.HASNEXT ()) {
      String Element = Itterator.Next ();
      System.out.Printf ("% S% n", element);
      If (Change) {
        if (element.equals ("collection")) {
          List.add ("New Row");
          List.Remove (Element);
        }
      }
    }
  }
  Public Static Void Main (String Args [])
  {
    new copyonwritearraylistexample ();
    System.exit (0);
  }
}

At the output, get this result:

cycle with change
 Java.
 J2EE
 J2SE.
 Collection.
 Concurrent
Cycle without change
 Java.
 J2EE
 J2SE.
 Concurrent
 New line

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions