- Processing
- p5.js
- Processing.py

- Processing Foundation

This reference is for Processing 3.0+. If you have a previous version, use the reference included with your software. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Javadoc.

Name | ### thread()
Examples |
    String time = "";

    void setup() {
      size(100, 100);
    }

    void draw() {
      background(0);
      // Every 30 frames request new data
      if (frameCount % 30 == 0) {
        thread("requestData");
      }
      text(time, 10, 50);
    }

    // This happens as a separate thread and can take as long as it wants
    void requestData() {
      JSONObject json = loadJSONObject("http://time.jsontest.com/");
      time = json.getString("time");
    }
Description | Processing sketches follow a specific sequence of steps: setup() first, followed by draw() over and over and over again in a loop. A thread is also a series of steps with a beginning, a middle, and an end. A Processing sketch is a single thread, often referred to as the "Animation" thread. Other threads' sequences, however, can run independently of the main animation loop. In fact, you can launch any number of threads at one time, and they will all run concurrently. Processing does this quite often, such as with library functions like captureEvent() and movieEvent(). These functions are triggered by a different thread running behind the scenes, and they alert Processing whenever they have something to report. This is useful when you need to perform a task that takes too long and would slow down the main animation's frame rate, such as grabbing data from the network. If a separate thread gets stuck or has an error, the entire program won't grind to a halt, since the error only stops that individual thread. Writing your own thread can be a complex endeavor that involves extending the Java Thread class. However, the thread() method is a quick and dirty way to implement a simple thread in Processing. By passing in a String that matches the name of a function declared elsewhere in the sketch, Processing will execute that function in a separate thread.
Syntax |
    thread(name)
Parameters |
name | String: name of the function to be executed in a separate thread
Returns | void
Related | setup() draw() loop() noLoop()

Updated on June 20, 2015 10:37:34pm UTC