Home » Android » Execute multiple Asynctask

Execute multiple Asynctask

Posted by: admin November 1, 2017 Leave a comment

Questions:

I would like to implement a image processing Asynctask in Android. There is a condition – if the previous asyntask is processing locally, the current task should process on the server.

I tried 4 images, and set the Thread.sleep(1000) in side the local process section, expected the first one process locally and others on server. However, they are all processed locally. Am I wrong?

private class ProcessImageTask extends AsyncTask<ImageItem, Void, ImageItem>{
    @Override
    protected ImageItem doInBackground(ImageItem... params) {
        if(localProcessing==false){
            //**************processing locally*****************
            localProcessing = true;
            try {
                Bitmap bm = BitmapFactory.decodeFile(params[0].getBitmap());

                Bitmap croppedBitmap = getBitmap(getApplicationContext(), INPUT_SIZE, bm);
                final List<Classifier.Recognition> results = classifier.recognizeImage(croppedBitmap);

                String resultStr = results.toString();
                String trimResult = resultStr.substring(resultStr.indexOf("[")+1,resultStr.indexOf("]")).trim();

                String localId = params[0].getId();
                trimResult = trimResult.substring(0,trimResult.indexOf(")")) + " likely)";

                Bitmap thumbnail = getBitmap(getApplicationContext(), 50, bm);
                ImageItem tmp = new ImageItem(localId, imgToString(thumbnail), trimResult);

                Thread.currentThread();
                Thread.sleep(1000);
                localProcessing = false;
                return tmp;
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else {
            //****************processing on server*************************
            try {
                String ip = "192.168.1.3";
                int port = 8195;
                Bitmap bm = BitmapFactory.decodeFile(params[0].getBitmap());
                Bitmap croppedBitmap = getBitmap(getApplicationContext(), INPUT_SIZE, bm);
                String encodedImage = "/ID-BEGIN/" + ID + "/ID-END" + imgToString(croppedBitmap);

                try {
                    //**********Send request to server*********
                    Socket socket = new Socket(ip,port);

                    DataInputStream dis = new DataInputStream(socket.getInputStream());
                    DataOutputStream dout = new DataOutputStream(socket.getOutputStream());

                    byte [] messageToServer = encodedImage.getBytes();
                    dout.writeInt(messageToServer.length);
                    dout.write(messageToServer);

                    //Receive response from server
                    int length = dis.readInt();

                    if(length>0) {
                        byte [] message = new byte[length];
                        dis.readFully(message, 0, message.length);

                        String response = new String(message);
                        //Handler updateHandler.post(new updateUIThread(response));

                        Bitmap thumbnail = getBitmap(getApplicationContext(), 50, bm);
                        ImageItem tmp = new ImageItem(params[0].getId(),imgToString(thumbnail), extractServerMessage(response)+"@@");
                        return tmp;

                    }
                    socket.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    @Override
    protected void onPostExecute(ImageItem imageItem) {
        super.onPostExecute(imageItem);
    }
}

and I executes in a for loop

ImageItem it = pit.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, tmp).get();

Should I need to set the core pool size? Thanks a lot.

Answers:

Your call to AsyncTask.get() waits for the task to finish before returning, so you’re not actually running these in parallel, despite using the THREAD_POOL_EXECUTOR. You shouldn’t call get here, but instead rely on onPostExecute to communicate results back to your program.