Java.util.concurrent.CopyOnWriteArrayList

Source: Internet
Author: User

1 import java.util.ArrayList;2 import java.util.List;3 import Java.util.concurrent.ExecutorService;4 import java.util.concurrent.Executors;5 import java.util.concurrent.CopyOnWriteArrayList;6  7  Public classTestcalc {8     /**9 *readTen      */ One     Private Static classReadtask implements Runnable { AList<string>list; -   -          PublicReadtask (list<string>list) { the              This. List =list; -         } -   -          Public voidrun () { +              for(String str:list) { -System. out. println (str); +             } A         } at     } -     /** - *write -      * -      */ -     Private Static classWritetask implements Runnable { inList<string>list; -         intindex; to   +          PublicWritetask (list<string> List,intindex) { -              This. List =list; the              This. index =index; *         } $  Panax Notoginseng          Public voidrun () { - List.remove (index); theList.add (Index,"Write_"+index); +         } A     } the   +      Public voidrun () { -FinalintNUM =Ten; $          //list<string> List = new arraylist<string> ();//java.util.ConcurrentModificationException $copyonwritearraylist<string> list =NewCopyonwritearraylist<string>(); -          for(inti =0; i < NUM; i++) { -List.add ("Main_"+i); the         } -Executorservice Executorservice =Executors.newfixedthreadpool (NUM);Wuyi          for(inti =0; i < NUM; i++) { theExecutorservice.execute (Newreadtask (list)); -Executorservice.execute (Newwritetask (list, i)); Wu         } - Executorservice.shutdown (); About     } $   -      Public Static voidMain (string[] args) { -         NewTestcalc (). run (); -     } A } +  the /* -  $ //list<string> List = new arraylist<string> (); the Exception in Thread "pool-1-thread-11" Java.util.ConcurrentModificationException the  the At java.util.arraylist$itr.checkforcomodification (Unknown Source) the At Java.util.arraylist$itr.next (Unknown Source) - At Testcalc$readtask.run (testcalc.java:19) in At Java.util.concurrent.ThreadPoolExecutor.runWorker (Unknown Source) the At Java.util.concurrent.threadpoolexecutor$worker.run (Unknown Source) the At Java.lang.Thread.run (Unknown Source) About */

Java.util.concurrent.CopyOnWriteArrayList

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.