Loading...
Logo
Processing Forum

Conditional Threads?

in Programming Questions  •  1 year ago  
Hi-
I am in the processing of making my code run faster and smoother as I have several sections of identical code in my program because I can not pass the values of variables up the chain. So, I am trying to create some threads that only run when a key is pressed or when a mouse is clicked...Would it be more efficient to create a thread or something else? Also how would I go about adding the conditional part? In the example below I believe I would alter the run() command but so far I have been unsuccessful in my attempts.

Copy code
  1. class TxtChange extends Thread {
                  boolean active;//running status
                  PApplet p;// to run serial ports as well
               
                  TxtChange() {//initial setting
                    active = false;
                  }
               
                  void start() {//start command
                    active = true;
                    super.start();
                  }
               
                  void run () {//to keep running this makes a loop how to get conditional
                      while(true){
                        if(active){
                         
                            execute();
                         
                        }else
                        break;
                      }
                   
                  }
               
                  TxtChange(PApplet p) {
                    this.p = p;      // store the reference to PApplet           
                    active = false;
                  }
               
                  void execute() {// what it does
                     if(keyPressed){
                       if (mouseX>=406 && mouseX<=586 && mouseY>=45 && mouseY<=70){
                         if(key==BACKSPACE){
                           Additional = Additional.substring(0, Additional.length()-1);
                         }
                         else if(key==DELETE){
                           Additional = "";
                         }
                         else if(keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT){
                           Additional = Additional + key;
                         }
                       }
                       else if (mouseX>=113 && mouseX<=293 && mouseY>=45 && mouseY<=70){
                         if(key==BACKSPACE){
                           Commandt = Commandt.substring(0, Commandt.length()-1);
                         }
                         else if(key==DELETE){
                           Commandt = "";
                         }
                         else if(keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT){
                           Commandt = Commandt + key;
                         }
                       }
                     }             
                   delay(80);//need to pause to not take all the processor memory
                  }
               
                  boolean isActive() {// no clue
                    return active;
                  }
               
                  void quit() {//how to end it
                    active = false;
                    interrupt();
                  }
                }

Thank you~

Replies(2)

Yes, you would modify the run() method... specifically, the if statement. If you want to check to see if the mouse is pressed, than something like this will do:

Copy code
  1. if(active && mousePressed)

However, I am unsure as to why you break out of the infinite loop if the thread is not active... do you call start() multiple times? It really depends on your implementation of the thread...
I was thinking of minimizing the time each thread is active to allow others to run faster, but I just realized that unless I want to be turning the thread on and off, which for the keyPressed I don't, then I might as well leave it looping. I will use the mouse pressed in for another piece where I can call start.

-thank you so much!!