Pessoal estava mexendo com threads e surgiu um problema que acredito ser de sincronização das thread que consomem um arraylist, pesquisei no google e cheguei ao seguinte código
[code]
/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package teste;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
*
-
@author rodrigo
*/
public class Teste {public ArrayList array;
public Teste(){
array = new ArrayList();
for(int i =0; i< 10000; i++){
array.add(i);
}
Teste1 t1 = new Teste1();
t1.nome = “T1”;
Teste1 t2 = new Teste1();
t2.nome = “T2”;
Teste1 t3 = new Teste1();
t3.nome = “T3”;Thread th1 = new Thread(t1); Thread th2 = new Thread(t2); Thread th3 = new Thread(t3); th1.start(); th2.start(); th3.start(); System.out.println(array);
}
public static void main(String args[]){
Teste t = new Teste();
}class Teste1 implements Runnable{
public String nome;
public void run(){
List list = Collections.synchronizedList(array);Iterator i = list.iterator(); while (i.hasNext()) { int valor = (Integer) i.next(); i.remove(); System.out.println(nome+" removeu = "+valor); } } }
}[/code]
porém ele ainda esta dando o erro de Exception in thread “main” java.util.ConcurrentModificationException
alguém poderia me dar uma luz?