Currently I have a Thread running a Socket listening for connections. When it receives a connection, it needs to upload data gathered in the main thread (i.e. grab data from main thread). However I pass an instance of the Object, but it’s never updated with the data that’s collected while waiting for a connection.
Is there a proper way to do this? I’ve googled around and can’t seem to find a concrete answer.
Could someone point me in the right direction?
Hopefully this makes sense, but i’ll try to explain more with an examples.
class MainThread {
private void MainThread() {
SomeObj obj = new SomeObj("DATA Needed");
SecondThread second = new SecondThread(obj);
second.start();
}
}
class SecondThread extends Thread {
SomeObj obj;
public void SecondThread(Object obj) {
this.obj = obj;
}
public void run() {
//Listening for connection
//Connection get!
//Get updated data (Object obj) from main thread.
//Upload
}
}
I appreciate any help you can give me. Please let me know if I am approaching this completely wrong! I would rather learn AND get answers than just get answers.
Thanks so much!
This doesn’t make sense as you don’t get information from Threads, you get information and communicate with Objects. That’s a big difference. You need to pass an instance of the object that you need information from into the second object that needs this information, perhaps as a parameter in its constructor. Then you would set a field in your SecondThread class with this instance and you can call methods on it.
i.e.,
Hopefully you’re not actually using an Object type but rather a much more specific type of object, right?