Skip to content

Instantly share code, notes, and snippets.

@benfarahmand
Created November 12, 2013 18:11
Show Gist options
  • Save benfarahmand/7435866 to your computer and use it in GitHub Desktop.
Save benfarahmand/7435866 to your computer and use it in GitHub Desktop.
Threading (multithreading) in processing, source code example
ThreadedClass myThread;
int drawCounter;
void setup(){
myThread = new ThreadedClass(50); //set the thread loop to wait every 50 milliseconds
drawCounter = 0;
myThread.start();
}
void draw(){
println("Draw Loop: " + drawCounter);
drawCounter++;
}
public class ThreadedClass extends Thread {
private int wait;
private int counter;
private boolean running;
public ThreadedClass (int wt) {
wait = wt;
running = false;
}
public void start() {
running = true;
super.start();
}
public void run() {
while (running) {
//Place whatever you want to run here
println("Thread Loop: " + counter);
counter++;
try {
sleep((long)(wait));
}
catch (Exception e) {
}
}
}
public void quit()
{
running = false;
interrupt();
// Now sleep a while to avoid memeory errors
try {
sleep(100l);
}
catch (Exception e) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment