I'd like to know how to call different functions cross thread in Java. Right now the way I'm doing it is to write my run() function of my thread as so
public volatile boolean invokeMyFunction = false;
public void run() {
while(true) {
if(invokeMyFunction) {
MyFunction();
invokeMyFunction = false;
}
}
}
and if I want to run the function MyFunction() from outside that thread write "whateverobject.invokeMyFunction = true" and it will run my function from within the thread because that loop will pick it up. This works great for me, but it uses 100% of my CPU because of that while(true) loop. I could fix that by just slapping a Thread.sleep(1000) inside the loop but that seems messy and I can't help but to believe there's a better way of doing this.