PThreading: A framework for multithreaded drawing in Processing
Getting started, motivation, download and example usage available from Github.
Natively, Processing runs on a single thread, which can lead to poor performance on draw-heavy sketches.
In short, the framework consists of two classes: PThread
and PThreadManager
. Override the PThread
class with Processing code and then add instances of your new class to a PThreadManager; this will run the instances as threads that draw into your Processing sketch in a safe multithreaded manner.
Example (11 threads; 5k particles each):
…from this sketch code (I’m leaving out the implementation of the Gravicubes class here – full example available here):
PThreadManager manager;
public void setup() {
manager = new PThreadManager(this);
manager.addThread(Gravicubes.class, 11, 60, 5000); // 11 threads targetting 60fps; 5000 is arg for # particles
}
public void draw() {
fill(255, 100);
rect(0, 0, width, height);
manager.draw();
}