Producer/Consumer model

edited December 2014 in How To...

Hey, long time lurker first time poster. I have some questions about an idea I have to implement the java producer consumer model in a processing project. In a nutshell, I would have a Processing object for graphical output, and then a set of threaded objects updating properties like their position, speed etc. These would communicate asynchronously with the Processing object using a shared queue of commands using the following code...

The shared queue of input args that represent user input
Queue inputQ = new LinkedList();

ShapeCoordinator sc = new ShapeCoordinator(inputQ); ProcessingCoordinator pc = new ProcessingCoordinator(inputQ);


The processing object in embedded in a Frame within the ProcessingCoordinator (as per, which Produces InputQ objects on the shared Queue in response to user input (mousemove/click/presses/mouseX/mouseY etc) on the embedded processing PApplet. ShapeCoordinator is a Thread that consumes these objects on notify() and uses the info to update the status of Shape objects. The ProcessingCoordinator reads the resulting state of the objects on notify(), and updates the PApplet graphics.

I want to do this to decouple Shape objects and the processing object as much as possible, eventually I want the Shape Object algorithms to connect to other non-processing code.

Does anyone think this model is an interesting way to use Processing? One trouble I've come across is that I can't figure out how to pass the shared queue InputQ to the embedded PApplet object so it can update it on user input, I can pass the queue to the applet, but the PApplet seems to nullify or ignore the object when it starts. Has anyone encountered this and resolved it?



  • Answer ✓

    I can't figure out what's being asked! Async data structures are diff. stuff and each case has its own particular way to implement in order to reach highest performance & reliance!

    I guess ArrayBlockingQueue is a good option if 1 doesn't wanna use synchronize.
    Otherwise, ArrayDeque is much better than LinkedList! o->

  • I did get a bit carried away there. I've since found some guides describing the Model View Controller model, which looks exactly like what I want to implement using Processing as the view.

  • edited December 2014

    btw, thanks for taking the time to read my post

Sign In or Register to comment.