this is my thread:
@Override
public void run() {
Log.d("ConnectionThread","Starting Server Connection");
try {
while(isThereActivityRunning()) {
if(isInternetOn == true){
Log.d("ConnectionThread", "Internet is On. Sending Http request");
results = sendGetMessage();
b.putString("results", results);
receiver.send(2, b);
}
else {
Log.d("ConnectionThread","Internet is Off. Sleeping");
}
ConnectionThread.sleep(timeInterval);
}
} catch (InterruptedException e) {
results = e.toString();
}
Log.d("ConnectionThread","Server Connection Finished");
}
The isInternetOn boolean is from an event listener I’ve made, and it’s value is changed by the state of the internet.
For some reason when the internet is on (isInternetOn == true) it does the if condition and then go to the else condition..
I can’t figure out why it is doing this. Thanks!
Not sure I am parsing your question correctly but you need to make sure that the values that you are checking inside of
isThereActivityRunning()have been marked asvolatileor have beensynchronizedif it is accessed in multiple threads. Same goes forisInternetOn. If your thread is not seeing this value change and if it is changed by another thread then this is probably the issue. You can also use theAtomicBooleanand other classes to share values between threads.