Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Second AsyncTask not executing

I have 2 AsyncTask, one which is creating a socket connections and anotherone that is transmitting objects using those sockets. my code is this:

try {
        connectat = true;
        transmitter = new SocketTransmitter();
        transmitter.execute();
        connector = new socketConnector();
        connector.execute(owner);

        this.open();
    } catch (IOException e) {

However, the AsyncTask called socketConnector is never created or executed. I tried to change the order but then transmitter is not created or executed...

Whats wrong with that?

like image 576
Albert Pasaret Avatar asked Nov 30 '12 15:11

Albert Pasaret


People also ask

How many times an instance of AsyncTask can be executed?

AsyncTask instances can only be used one time.

What are the problems in AsyncTask?

In summary, the three most common issues with AsyncTask are: Memory leaks. Cancellation of background work. Computational cost.

What is the replacement of AsyncTask?

Alternative 1: Using Executor and Handler The executor will help in performing any task in the background and the handler will help to make UI changes.

In which thread AsyncTask function will execute?

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .


1 Answers

I hated it when HONEY COMB changed the multiple AsyncTask execution from concurrent to sequential. So every time I execute an AsyncTask, I do something like this.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
    task.execute();
}

But the thread pool size is 5, if you add the sixth task, it will be added in a queue, and will not be executed until one of the 5 thread has finished.

like image 77
coocood Avatar answered Oct 10 '22 14:10

coocood