Quote:If I call my thread's constructor in setup() and loop the run() function like you mentioned, that run() function will continue looping while the rest of my program moves on to the draw() method. Is that correct? In other words, my program won't endlessly stay in setup() until that run() method finishes, right?
My mistake we will remain forever in setup() the solution is in the code example below.
Quote:Do you think I could still run into problems with the program trying to check a text file while the other thread is trying to overwrite existing text files?
Yes
One way round this is to have a variable that is shared between the threads that indicates who has access to the data.
I assume that all your code is in a single file, if that is the case then a variable created outside the your Thread class will be available throughout the code.
Code:
boolean locked = true;
boolean updating_enabled = true;
void setup(){
// create an object (eg myTwitterReader) from the
// class that extends Thread
// call myTwitterReader.start()
// do not call run()
}
void stop(){
updating_enabled = false;
}
void draw(){
if(locked == false){
locked = true;
// do the using data to draw stuff here
locked = false;
}
}
In the run method
Code:
void run(){
while(updating_enabled == true){
if(locked == false){
locked = true;
// read the Twitter feeds
locked = false;
try{
sleep(1000); // sleep for 1 second give draw() a chance
}
catch (Exception e){
}
} // end of if statement
}// end of while loop
}
The thread will run until you set updating_enabled to false
The stop method is called by Processing when you end the program otherwise the running thread might stop your program closing.
Using this you don't need to use a text file you can read the Twitter feeds into a String or whatever since draw() and run() cannot access the data at the same time.
I typed this directly into this post so might be odd typo error.
Hope this helps.