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