# Message Queue with oscP5 not treating messages as objects

**URL:** <https://discourse.processing.org/t/message-queue-with-oscp5-not-treating-messages-as-objects/21328>\
**Category:** Libraries\
**Created:** [May 26, 2020, 9:28pm UTC](https://discourse.processing.org/t/message-queue-with-oscp5-not-treating-messages-as-objects/21328 "2020-05-26T21:28:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Merk](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@Merk](https://discourse.processing.org/u/Merk)\
**Post date:** [May 26, 2020, 9:28pm UTC](https://discourse.processing.org/t/message-queue-with-oscp5-not-treating-messages-as-objects/21328/1 "2020-05-26T21:28:38Z")

</div>

Hi,

Long story, but I’m trying to implement a queue handler for incoming OSC messages, as I’m getting concurrency issues.

There was a really helpful example from feb 2016 on the archived forum where user GoToLoop provided some really helpful example code that seems to do what I’m after.

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/11634/concurrent-modification-exception-trying-to-add-objects-on-osc-event)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

BUT when I try to run the code I get an error saying  
**"cannot convert from element type Object to OscMessage"**

I’ve tried a few things, and it looks like the message object getting appended to the ArrayList in the oscEvent function is being treated as an Object, rather than an OscMessage, but I can’t figure out how to fix it.

Can anyone please help explain why the example code isn’t working - has something changed in [Processing.org](http://Processing.org), or in the OscP5 library since this was originally shared?

thanks in anticipation - I really want to find a way to solve the concurrency issue or my project won’t ever work properly.

```auto
import oscP5.*;
import netP5.*;
 
OscP5 oscP5;
ArrayList oscMessageQueue = new ArrayList();
 
void setup() {
  oscP5 = new OscP5(this, 12000);
}
 
void draw() {
   
  // take care of the OSC messages once per frame
  dispatchOSCMessages();
   
  // Do the rest of your rendering here, it won't be bothered by OSC messages showing up.
}
 
// Handle all the enqueued OSC messages at once.
void dispatchOSCMessages() {
  synchronized(oscMessageQueue) {
    for(OscMessage message : oscMessageQueue) {
      // Do things here that require modifying lists or other actions
      // which should never happen half-way through the draw function.
    }
     
    // clear the list of messages.
    oscMessageQueue.clear();
  }
}
 
// Add the OSC messages to the queue to be handled later.
void oscEvent(OscMessage message) {
  synchronized(oscMessageQueue) {
    oscMessageQueue.add(message);
  }
}

```

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [May 27, 2020, 3:16am UTC](https://discourse.processing.org/t/message-queue-with-oscp5-not-treating-messages-as-objects/21328/2 "2020-05-27T03:16:33Z")

</div>

Based on the [ArrayList](https://processing.org/reference/ArrayList.html) docs, your issue is solved by defining the ArrayList to store _OscMessage_ objects:

`ArrayList<OscMessage> oscMessageQueue = new ArrayList<OscMessage>();`

Maybe it was overlooked in the example, but this should work.

Kf

---

<div class="post-metadata">

**Author:** ![Merk](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@Merk](https://discourse.processing.org/u/Merk)\
**Post date:** [May 27, 2020, 1:40pm UTC](https://discourse.processing.org/t/message-queue-with-oscp5-not-treating-messages-as-objects/21328/3 "2020-05-27T13:40:22Z")

</div>

Fantastic, thanks so much - that works!
