Reputation: 153
I have an Asynctask class and a I have an array inside that class which is to be filled by the doInBackground(Void...)
and then I am accessing that variable from other activities. My question is will the thread ever dies or finishes execution if I have a refrence to that class variable from other activities?
For example:
public class SendReport extends AsyncTask<Void,Void,Boolean> {
static ArrayList data=new ArrayList<>();
@Override
protected Boolean doInBackground(Void... params) {
sendReport();
return null;
}
public void sendReport() {
//Do something here to fill the Array
}
And then from the activity I am using the static variable:
SendReport.data.get(2);
Upvotes: 0
Views: 751
Reputation: 27115
A thread, t
, runs until;
run()
method returns, orrun()
method throws an exception, ort.stop()
(Bad Idea), orSystem.exit(...)
(not-so-great idea).The fact that some other thread is able to access variables (e.g., your data
variable) that may have been updated by thread t
has no bearing on when or whether thread t
will stop running.
Upvotes: 0
Reputation: 1912
I believe static variables are related to class not the object itself. If you read the array from anywhere it should not keep the thread running.
Having said that, you have to be really careful with what you are storing in the array. Because the array is static the objects in the array will be there as long as the jvm is running (This is unless they are explicitly removed from the array). I would strongly advice against doing whatever you are doing this way
Upvotes: 1