Reputation: 481
I have a basic question about Async task. I'm a beginner in Android programming, sorry for this question.
I'm going to open a socket in doinbackground.
doInBackground(... ) {
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
try {
socket = new Socket(192.168.0.1, 2000);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
}}
What happens to a socket when AsyncTask has finished? (As soon as doInBackground and OnPostExecute has passed.)
Is the Socket still available? Or will it removed by the Garbage Collector?
Next question, but actually the same background.
What happens to an instance of a class which I Instantiate in doInBackground after AsyncTask has finished? (As soon as doInBackground and OnPostExecute has passed.)
doInBackground(... ) {
IPConnection ipcon = new IPConnection();
}
---------------------------------------------------------------------
Edit:
How can I create a reference from a object in Asynctask to the MainActivity?
Edit2:
Is that a reference to the main thread? Would the objects not be removed by the Garbage Collector in that Code Example?
public class ClientActivity extends Activity {
private IPConnection ipcon;
private Socket Testsocket;
public class IPConnection extends AsyncTask<String, String, IPConnection> {
@Override
protected IPConnection doInBackground(String... message) {
ipcon = new IPConnection();
ipcon.run();
return null;
}
}
}
Thank you in advance.
Upvotes: 0
Views: 2986
Reputation: 11112
Is the Socket still available? Or will it removed by the Garbage Collector?
No the socket will be unavailable and will be removed by the garbage collector because it doesn't hold any reference
What happens to an instance of a class which I Instantiate in doInBackground after AsyncTask has finished? (As soon as doInBackground and OnPostExecute has passed.)
same as above the ipconnection doesn't hold any reference so it'll be collected by garbage collector
if you want to pass it to the activity you can create an interface
public interface AsyncResultPasser {
void passSocket(Socket socket);
void passIPconnection(IPConnection ipcon);
}
and then in your asynctask class you have to add
public AsyncResultPasser delegate = null;
and don't forget to set it first before you execute your asynctask
public class YourActivity implements AsyncResponse{
YourAsyncTask asyncTask = new YourAsyncTask ();
@Override
public void onCreate(Bundle savedInstanceState) {
asyncTask.delegate = this;
}
void passSocket(Socket socket){
//you can get your socket here
}
void passIPconnection(IPConnection ipcon){
//you can get your ipconnection here
}
}
and to call it just simply use delegate.passSocket(socket)
and delegate.passIPconnection(ipcon)
I hope my answer can helps you :)
Upvotes: 2
Reputation: 17284
As soon as doInBackground()
finishes, all local instances will be available for garbage collection, unless you pass one of those to onPostExecute()
by returning it form doInBackground()
. Such instances will be available after onPostExecute()
finishes. But again only if you don't send these instances further somewhere.
Upvotes: 0