Summing up some of the discussion, and also coming back to the main topic, I wonder if this is close to the render() function. I think PhiLho brings up really relevant point because I would like to have expensive operation like sphere() to be deferred when it is slowing down the rest of the operations.
Maybe it's meaningless for now because it still uses a flag to lock. I thought of subclassing PApplet and merging the threads but can't quite figure out how that would work within Processing:
Code:
//enum Appmode {SETUP, DRAW, LOCK}
//Appmode appmode;
String appmode;
PApplet pa;
void setup(){
size(500,500, P2D);
smooth();
appmode = "SETUP";
}
void draw(){
if(appmode.equals("SETUP")){
pa = this;
ThreadedDraw threadedDraw = new ThreadedDraw();
new Thread(threadedDraw).start();
appmode = "LOCK";
}
else if(appmode.equals("DRAW")){
println("drawing");
}
else if(appmode.equals("LOCK")){
println("locked");
}
}
class ThreadedDraw implements Runnable{
void run(){
if(appmode.equals("SETUP")){
//throw error
}
else if(appmode.equals("DRAW")){
println("threadlocked");
}
else if(appmode.equals("LOCK")){
draw();
}
}
void draw(){
while(true){
appmode = "LOCK";
println("locking);
pa.background(130);
pa.ellipse(10, 10, 10, 10);
render();
pa.rect(200,200,10,10);
render();
}
}
void render(){
appmode = "DRAW";
}
}
I also realize I've been coding in java5, and I can't use enum in Processing
=== edit ==
I found out that it doesn't work as I thought