简体   繁体   English

执行多个Asynctask

[英]Execute multiple Asynctask

I would like to implement a image processing Asynctask in Android. 我想在Android中实现图像处理Asynctask。 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. 我尝试了4张图像,并将Thread.sleep(1000)设置在本地进程部分,期望本地第一个进程以及服务器上的其他进程。 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 我在for循环中执行

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

Should I need to set the core pool size? 我是否需要设置核心池大小? Thanks a lot. 非常感谢。

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 . 您对AsyncTask.get()调用在返回之前等待任务完成,因此尽管使用THREAD_POOL_EXECUTOR ,您实际上并未并行运行它们。 You shouldn't call get here, but instead rely on onPostExecute to communicate results back to your program. 您不应该调用get here,而是依靠onPostExecute将结果传达回程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM