# Concurrent Modification Exception question

**URL:** <https://discourse.processing.org/t/concurrent-modification-exception-question/14905>\
**Category:** Libraries\
**Created:** [October 23, 2019, 9:17pm UTC](https://discourse.processing.org/t/concurrent-modification-exception-question/14905 "2019-10-23T21:17:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![robbyc23](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/robbyc23/32/6856_2.png) [@robbyc23](https://discourse.processing.org/u/robbyc23)\
**Post date:** [October 23, 2019, 9:17pm UTC](https://discourse.processing.org/t/concurrent-modification-exception-question/14905/1 "2019-10-23T21:17:17Z")

</div>

Hello,

I’ve been figuring out the cause of my issue (adding to an array while simultaneously deleting from it), but can’t figure out how to work around it…yet.

I’ve repurposed some code from Shiffman’s Nature of Code to react on a clientEvent() and add a new box to a Box2D array. It will work fine for a random amount of time, but always hangs with a ConcurrentModificationException error.

clientEvent() seems to clash with displaying the boxes in the draw().

Could anyone give me a nudge in the right direction? Keep moving things around, but can’t quite hit on a solution today.

```auto

import processing.net.*;
import shiffman.box2d.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;

Client myClientBin1;

int counter = 0;

PImage img1;
PImage img2;
PImage img3;

// A reference to our box2d world
Box2DProcessing box2d;

// A list we'll use to track fixed objects
ArrayList<Boundary> boundaries;
// A list for all of our rectangles
ArrayList<Box> boxes;
ArrayList<BoxB> boxesB;

void setup() {
  size(800,600);
  myClientBin1 = new Client(this, "139.XXX.XXX.XXX", 1999);
  smooth();
  
  img1 = loadImage("1.png");
  img2 = loadImage("2.png");
  img3 = loadImage("background.jpg");
  
  // Initialize box2d physics and create the world
  box2d = new Box2DProcessing(this);
  box2d.createWorld();
  // We are setting a custom gravity
  box2d.setGravity(0, -20);

  // Create ArrayLists	
  boxes = new ArrayList<Box>();
  boxesB = new ArrayList<BoxB>();
  boundaries = new ArrayList<Boundary>();

  // Fixed boundaries
  boundaries.add(new Boundary(width/2,height,width,0));
}

void draw() {
  background(img3);
  stroke(0);
  fill(0);
  ellipse(width/2, 40, 100,40);
  text(frameRate, 750, 20);
  // We must always step through time!
  box2d.step();

  // Display all the boxes
  for (BoxB bB: boxesB) {
    bB.display();
  }
  for (Box b: boxes) {
    b.display();
  }

  // Boxes that leave the screen, we delete them
  // (note they have to be deleted from both the box2d world and our list
  for (int i = boxes.size()-1; i >= 0; i--) {
    Box b = boxes.get(i);
    if (b.done()) {
      boxes.remove(i);
    }
  }
  for (int i = boxesB.size()-1; i >= 0; i--) {
    BoxB bB = boxesB.get(i);
    if (bB.done()) {
      boxesB.remove(i);
    }
  }
}

void clientEvent(Client someClient) {
    float boxVersion = random(1,3);
    if (int(boxVersion) == 1){
        Box p = new Box((width/2)-16,30);
        delay(1);
        boxes.add(p);
    }
    else{
        BoxB q = new BoxB((width/2)-16,30);
        delay(1);
        boxesB.add(q);
    } 
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 23, 2019, 11:56pm UTC](https://discourse.processing.org/t/concurrent-modification-exception-question/14905/2 "2019-10-23T23:56:42Z")

</div>

> [@robbyc23](#):
>
> Could anyone give me a nudge in the right direction?

> [@Synchronize access to array](https://discourse.processing.org/t/synchronize-access-to-array/14895/4):
>
> Most Processing libs that have callback names w/ the word “event” on them are indeed threaded. thread There are many ways to workaround threading access/mutation; but I prefer to start w/ the simplest 1s before attempting more advanced techniques. man_technologist If your sketch just needs to refresh the draw() when a new data arrives, you can simply use this [noLoop()](https://processing.org/reference/noLoop_.html)/[redraw()](https://processing.org/reference/redraw_.html) combo: bulb

---

<div class="post-metadata">

**Author:** ![robbyc23](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/robbyc23/32/6856_2.png) [@robbyc23](https://discourse.processing.org/u/robbyc23)\
**Post date:** [October 24, 2019, 1:59pm UTC](https://discourse.processing.org/t/concurrent-modification-exception-question/14905/3 "2019-10-24T13:59:37Z")

</div>

_ **This solution didn’t work** _

Nice! I had seen that reply on another thread, but it hadn’t worked (the way I’d tried).  
Just saw your nudge before falling asleep. Woke up this morning with a solution. Thanks GoToLoop.

Now stopping draw() where the error occurred previously. Letting the business be done with the arrays, and then loop()-ing again. Testing now…seems stable.

```auto
  noLoop();
  // Display all the boxes
  for (BoxB bB: boxesB) {
    bB.display();
  }
  for (Box b: boxes) {
    b.display();
  }
  loop();

```

---

<div class="post-metadata">

**Author:** ![robbyc23](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/robbyc23/32/6856_2.png) [@robbyc23](https://discourse.processing.org/u/robbyc23)\
**Post date:** [October 24, 2019, 3:54pm UTC](https://discourse.processing.org/t/concurrent-modification-exception-question/14905/4 "2019-10-24T15:54:19Z")

</div>

I take that back. Just crashed again with same error.

Back to the drawing board…

Any other nudges out there?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 24, 2019, 8:57pm UTC](https://discourse.processing.org/t/concurrent-modification-exception-question/14905/5 "2019-10-24T20:57:37Z")

</div>

> [@robbyc23](#):
>
> Letting the business be done with the arrays, and then **loop()**-ing again.

Neither **loop()**, **noLoop()** or **redraw()** got any supernatural power to instantaneously pause, interrupt or resume an already called back **draw()**!

They’re respectively equivalent to:

1. `looping = true;`
2. `looping = false;`
3. `redraw = true;`

As you can see now, they merely mutate 2 Processing system-wide `boolean` fields.

Which can only affect whether the next **draw()** is called back or not.

Current **draw()** is impervious to any of them.

Regardless, the **noLoop()**/**redraw()** combo (notice it’s not **noLoop()**/**loop()**) can only be applied to cases where the canvas can await to be updated until a next event arrives.

It seems like your sketch here is supposed to be a fluid 60 FPS continuous animation.

Therefore it can’t use the simplest **noLoop()**/**redraw()** event-based approach after all.

So let’s go on to the 2nd simplest approach now, which is `boolean` flag-based.

As the name implies, you’re gonna need a `boolean` global field for it.

Let’s call it: `boolean makeNewBox;`

Now, rather than **add()** a `new` Box or BoxB inside **clientEvent()** (which is btW called back by another Thread), we do it inside **draw()** (which is called back by the “Animation” Thread) each time _makeNewBox_ becomes `true`.

This time, the only thing we do inside **clientEvent()** is assigning `true` to _makeNewBox_ in order to flag the next **draw()** callback:

```auto
void clientEvent(final Client c) {
  makeNewBox = true;
}

```

Inside **draw()** we now continuously check _makeNewBox_.

Once it becomes `true`, we immediately set it back to `false`.

And then we do what previously was done inside **clientEvent()**:

```auto
if (makeNewBox) {
  makeNewBox = false;

  final int x = (width >> 1) - 16, y = 30;

  if (random(1) < .5) boxes.add(new Box(x, y));
  else boxesB.add(new BoxB(x, y));
}

```

---

<div class="post-metadata">

**Author:** ![robbyc23](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/robbyc23/32/6856_2.png) [@robbyc23](https://discourse.processing.org/u/robbyc23)\
**Post date:** [October 25, 2019, 10:43am UTC](https://discourse.processing.org/t/concurrent-modification-exception-question/14905/6 "2019-10-25T10:43:04Z")

</div>

Very nice.

Learned a lot about loop()/redraw()/loop(), and their limitations.

Was trying to return the random number from the clientEvent(), but this method is much better. Neat code.

Appreciate the hand-holding! Time was tight.
