I have a class (lets say SocketClass) which extends AsyncTask (I am using Sockets, that's why I am using AsyncTask). I am calling that class on a different class which runs on the main thread.
SocketClass socketClass = new SocketClass(input);
socketClass.execute();
System.out.println(socketClass.getOutput());
This is my SocketClass
public class SocketClass extends AsyncTask < String, Void, Void > {
int output;
int input;
public Offload(int input) {
this.input = input;
}
public int getOutput() {
return output;
}
public void doSomething() {
// sockets related code
// set value to the output variable
}
@Override
protected Void doInBackground(String...voids) {
doSomething();
return null;
}
}
When I run the application System.out.println(socketClass.getOutput()); will get executed before a value fetch to the output variable. Is it possible to execute System.out.println(socketClass.getOutput()); only after fetching a value to output variable in doSomething() method? There are some solutions in Stackoverflow such as Solution1 and Solution2, but I am afraid to use these because I dont know whether it will affect badly to the application since we want to hold some process which are in the main thread