# PApplet call order (multi-threading thread safety)

**URL:** https://discourse.processing.org/t/papplet-call-order-multi-threading-thread-safety/21447
**Category:** Processing
**Created:** [May 31, 2020, 9:57am UTC](https://discourse.processing.org/t/papplet-call-order-multi-threading-thread-safety/21447 "2020-05-31T09:57:04Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![mgood7123](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@mgood7123](https://discourse.processing.org/u/mgood7123)
#### Post date: [May 31, 2020, 9:57am UTC](https://discourse.processing.org/t/papplet-call-order-multi-threading-thread-safety/21447/1 "2020-05-31T09:57:04Z")

</div>

what is the call order for the following methods:

- setup()
- draw()
- mousePressed()
- mouseDragged()
- mouseReleased()
- // related keyboard methods

in particular:

am i correct to assume that draw() will pause during the run on any non-draw() method

for example:

if i want to implement a graphics save/restore feature in mouseReleased()

that must assume that no draw() calls will be made during the save and restore

will this be possible and if so how would i implement it if draw() is still being called for the duration of other main functions

for example

upon state restore, the current graphics does not have unexpected values in it related to graphical calculations

for example

- timer is 3 on save
- draw() gets called and increases timer to 4
- restore is called
- draw is called
- draw expects time to be 4 and increase to 5 but is instead 3

---

<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: [May 31, 2020, 11:26am UTC](https://discourse.processing.org/t/papplet-call-order-multi-threading-thread-safety/21447/2 "2020-05-31T11:26:28Z")

</div>

> [@mgood7123](#):
>
> Am I correct to assume that **draw()** will pause during the run on any non-**draw()** method?

All of those callbacks are run synchronously by the same “Animation” Thread.

It’s not that **draw()** “pauses” for the resolution of the input callbacks, but rather that **draw()** has already finished for that frame iteration.

---

<div class="post-metadata">

### Author: ![mgood7123](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@mgood7123](https://discourse.processing.org/u/mgood7123)
#### Post date: [June 1, 2020, 6:15am UTC](https://discourse.processing.org/t/papplet-call-order-multi-threading-thread-safety/21447/3 "2020-06-01T06:15:13Z")

</div>

so… if all the callbacks are called synchronously, then all methods would block one another correct? eg something like this

```auto
Thread animation() {
    applet.setup();
    while(true) {
        applet.draw();
        if (mousePressed()) applet.mousePressed();
        if (mouseDragged()) applet.mouseDragged();
        if (mouseReleased()) applet.mouseReleased();
        if (!applet.ShouldLoop) break;
    }
}

```

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [June 1, 2020, 7:42am UTC](https://discourse.processing.org/t/papplet-call-order-multi-threading-thread-safety/21447/4 "2020-06-01T07:42:22Z")

</div>

> [@mgood7123](#):
>
> then all methods would block one another correct?

True. That is why we don’t execute time consuming blocks of code inside an event handler.

In the code you provide there appears to be two applet instances. If that is correct then each applet has its _own event-handling thread_ and you shouldn’t be calling the draw method or event handlers of one applet instance from another instance.

---

<div class="post-metadata">

### Author: ![mgood7123](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@mgood7123](https://discourse.processing.org/u/mgood7123)
#### Post date: [June 1, 2020, 7:44am UTC](https://discourse.processing.org/t/papplet-call-order-multi-threading-thread-safety/21447/5 "2020-06-01T07:44:57Z")

</div>

i dont know how the internals work but im assuming the thread and the Applet are seperate classes and as such it as a reference to the current running Applet, eg

`thisAppletInstance.setup()`

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [June 1, 2020, 7:56am UTC](https://discourse.processing.org/t/papplet-call-order-multi-threading-thread-safety/21447/6 "2020-06-01T07:56:55Z")

</div>

To get a reference to the running PApplet we use the Java keyword `this`. If you run the following code you will see the output `PApplet PApplet` which demonstrates this.

```auto
void setup(){
  size(400,400);
  String thisname = this.getClass().getSuperclass().getSimpleName();
  String name = this.getClass().getSuperclass().getSimpleName();
  println(thisname, name);
}

```

Are you trying to have multiple PApplets running? What are you hoping to achieve?

---

<div class="post-metadata">

### Author: ![mgood7123](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@mgood7123](https://discourse.processing.org/u/mgood7123)
#### Post date: [June 1, 2020, 8:48am UTC](https://discourse.processing.org/t/papplet-call-order-multi-threading-thread-safety/21447/7 "2020-06-01T08:48:46Z")

</div>

like… all i want to know is what functions will block the draw() call if any

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [June 1, 2020, 9:22am UTC](https://discourse.processing.org/t/papplet-call-order-multi-threading-thread-safety/21447/8 "2020-06-01T09:22:11Z")

</div>

The following answer applies to Java mode.

First we distinguish between _event capture_ and _event handlers_. When the mouse moves or a mouse button clicked or a key typed … then this is _captured_ by the OS which creates an event-data-object that is forwarded to the currently active application, our sketch perhaps.

Now our sketch will have several threads running one of which will add the event-data-object to a fifo queue (fifo = first in first out) to be processed later.

So now we come to the interesting bit - the main event-handling-thread (EHT). This thread handles both rendering and _event handling_ so when the thread is not rendering it will look remove the first event from the queue and depending on its type call the appropriate _event handler_ to process the event. the EHT might process several events between each frame render so you get

**draw** : event : event : **draw** : event : event : event : event : **draw** : event : **draw** : event

Events are processed in the order they were captured but there is no way to predict how many event will be handled between frames.

Now you have your answer - all _event handlers_ will block the draw method so it is important that large time consuming tasks are not performed inside the event handlers. If we do, not only will it block draw but it will prevent other events being processed, making the sketch unresponsive.

```auto
int eventCount = 0, maxEventCount = 0;
;

void setup() {
  size(320, 200);
  textSize(30);
}

void draw() {
  background(0);
  fill(255);
  text("" + eventCount, 20, 100);
  fill(255, 0, 0);
  maxEventCount = max(maxEventCount, eventCount);
  text("" + maxEventCount, 220, 100);
  eventCount = 0;
}

void mouseMoved() {
  eventCount++;
}

```

---

<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: [June 1, 2020, 10:33am UTC](https://discourse.processing.org/t/papplet-call-order-multi-threading-thread-safety/21447/9 "2020-06-01T10:33:44Z")

</div>

> [@Stumped by something simple](https://discourse.processing.org/t/stumped-by-something-simple/10057/9):
>
> Even though Processing enqueues input events in a separate Thread, it dequeues them in the main “Animation” Thread. So it’s safe to “draw” when using Processing’s own input callbacks. Now, if a 3rd-party library deals w/ hardware, its callbacks got a very high chance to happen in a separate Thread. Therefore, it’s highly unsafe to mutate the main canvas under a separate Thread. In order to find out which Thread is executing a method, just place this statement there: println(Thread.currentTh…

- [Is this a known bug? - Processing 2.x and 3.x Forum](http://Forum.Processing.org/two/discussion/24624/is-this-a-known-bug#Item_13)
- [push exceeding 32 use limit - Processing 2.x and 3.x Forum](http://Forum.Processing.org/two/discussion/15972/push-exceeding-32-use-limit#Item_12)

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [June 1, 2020, 10:48am UTC](https://discourse.processing.org/t/papplet-call-order-multi-threading-thread-safety/21447/10 "2020-06-01T10:48:36Z")

</div>

If you are interested in finding out what threads are running with your sketch then try the code in this [discussion](https://forum.processing.org/two/discussion/13671/processing-and-garbage-collection).
