# PThreading: A framework for multithreaded drawing in Processing

**URL:** https://discourse.processing.org/t/pthreading-a-framework-for-multithreaded-drawing-in-processing/14782
**Category:** Gallery
**Created:** [October 18, 2019, 8:35pm UTC](https://discourse.processing.org/t/pthreading-a-framework-for-multithreaded-drawing-in-processing/14782 "2019-10-18T20:35:34Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)
#### Post date: [October 18, 2019, 8:35pm UTC](https://discourse.processing.org/t/pthreading-a-framework-for-multithreaded-drawing-in-processing/14782/1 "2019-10-18T20:35:35Z")

</div>

### PThreading: A framework for multithreaded drawing in Processing

#### _Getting started_, motivation, download and example usage available from [Github](https://github.com/micycle1/PThreading).

* * *

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):

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/6cdd01e1738cfbbe3fd08bb25f5928bebf3a325e.jpeg)

…from this sketch code (I’m leaving out the implementation of the Gravicubes class here – full example available [here](https://github.com/micycle1/PThreading/tree/master/examples/Gravicubes)):

```auto
    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();
	}

```
