I'm having trouble accessing data in my main thread from my new thread. I can do this just fine without threading by just using the main class getters/setters. But when I try to launch a new thread I can no longer do so.
Main Class:
public class Driver extends Application{
//create socket handlers
Runnable myNewThread = new workerThread();
//variables
private String lastMessage = "";
//getters and setters
public String setMyVariable() {
this.MyVariable = MyVariable;
}
//main
public static void main(String[] args) {
//launch threads
new Thread(myNewThread).start();
}
NewThread Class:
public class workerThread implements Runnable{
public void run() {
Driver.setMyVariable("test");
}
I get the error "Cannot resolve symbol 'setMyVariable'" in my workerThread
class. As far as I can tell it's because my workerThread thread doesn't know what instance of Driver to refer to (there is only one instance, but it doesn't know that). Could someone help me understand what I'm missing? I've seen examples of declaring the new class within a function of the main class, but I'm trying to avoid this in the interest of code organization as my worker thread is going to be a bit large.