Got a code with Main Thread and myThread. When some button is pressed in Main Thread myThread.start()
is called. onCameraFrame
constantly gets and saves color values of frames to ArrayList<Double> rV
. In myThread i need sout(rV)
, do some stuff with it and clean rV every 6 seconds.
I used iterator to do such thing, but still i get java.util.ConcurrentModificationException
at sout
line in myThread. Note it happens at random time. For example after button was pressed sout might work just fine for 1 sec or for 5 minutes, and then - exception.
My suggestion is that rV is used by myThread and by Main Thread (onCameraFrame) at one time. So it collapses.
Need an advice. Struggling with this for hours.
Here's the code.
public class Camera extends Activity implements CvCameraViewListener2 {
@Override
public void onCreate(Bundle savedInstanceState) {
..
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
..
myThread = new Thread(mRunnable);
myThread.start();
}
..
}
Runnable mRunnable = new Runnable() {
@Override
public void run() {
Thread thisThread = Thread.currentThread();
while (myThread==thisThread) {
try {thisThread.sleep(6000);}
catch ..}
mButton.post(new Runnable() {
@Override
public void run() {
/*logging*/
if (!rV.isEmpty()){
System.out.println("rV"+"("+rV.size()+")={"+rV.toString()+"}");
}
*//*clean data*//*
for (Iterator<Double> it = rV.iterator(); it.hasNext();) {
while(it.hasNext()){
Double t = it.next();
it.remove();
}
}
}
});
}
}
};
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
..
if (condition) {
rV.add(somevalue);
}
return inputFrame.rgba();
}