Created
November 12, 2013 18:11
-
-
Save benfarahmand/7435866 to your computer and use it in GitHub Desktop.
Threading (multithreading) in processing, source code example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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