如何循环2 AsyncTasks?

时间:2016-03-26 04:34:27

标签: android loops android-asynctask

任何答案都会非常感激。

我有两个AsyncTasks,一个用于衡量下载速度,另一个用于衡量上传速度。我希望应用程序能够持续测量下载和上传速度,直到用户说停止为止。现在,我有一个切换按钮,由于OutOfMemoryError而导致崩溃:

togglebutton = (ToggleButton) findViewById(R.id.toggleButton);
togglebutton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            while (isChecked) {
                new Download.execute();
                new Upload.execute();
            }
        } else {
            // The toggle is disabled
        }
    }
});

当我点击切换按钮时,应用程序冻结然后崩溃。我假设创建我的Asynctasks的多个实例是导致问题的原因。有没有更好的方法来实现这个?

注意:我不认为doInBackground中的循环会起作用,因为Asynctasks需要以交替的方式运行,即Download.execute,Upload.execute,Download.execute,Upload.execute ...

这里是logcat:

03-25 23:31:36.261 15881-15888/com.kingrandesigns.uploadpicturetutorial E/art: Throwing OutOfMemoryError "Failed to allocate a 28 byte allocation with 0 free bytes and 3GB until OOM" (recursive case)
03-25 23:31:36.261 15881-15881/com.kingrandesigns.uploadpicturetutorial I/art: Alloc sticky concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 128MB/128MB, paused 25.421ms total 29.582ms
03-25 23:31:36.261 15881-15894/com.kingrandesigns.uploadpicturetutorial I/art: WaitForGcToComplete blocked for 29.847ms for cause Background
03-25 23:31:36.261 15881-16557/com.kingrandesigns.uploadpicturetutorial I/art: WaitForGcToComplete blocked for 877.665ms for cause Alloc
03-25 23:31:36.271 15881-15888/com.kingrandesigns.uploadpicturetutorial E/art: "JDWP" daemon prio=5 tid=3 Runnable
03-25 23:31:36.271 15881-15888/com.kingrandesigns.uploadpicturetutorial E/art:   | group="system" sCount=0 dsCount=0 obj=0x12c5f080 self=0xaf20e400
03-25 23:31:36.271 15881-15888/com.kingrandesigns.uploadpicturetutorial E/art:   | sysTid=15888 nice=0 cgrp=apps sched=0/0 handle=0xb4922200
03-25 23:31:36.271 15881-15888/com.kingrandesigns.uploadpicturetutorial E/art:   | state=R schedstat=( 2731136812 186756673 920 ) utm=267 stm=6 core=0 HZ=100
03-25 23:31:36.271 15881-15888/com.kingrandesigns.uploadpicturetutorial E/art:   | stack=0xb4366000-0xb4368000 stackSize=1012KB
03-25 23:31:36.271 15881-15888/com.kingrandesigns.uploadpicturetutorial E/art:   | held mutexes= "mutator lock"(shared held)
03-25 23:31:36.271 15881-15888/com.kingrandesigns.uploadpicturetutorial E/art:     at java.nio.ByteBuffer.wrap(ByteBuffer.java:113)
03-25 23:31:36.271 15881-15888/com.kingrandesigns.uploadpicturetutorial E/art:     at org.apache.harmony.dalvik.ddmc.ChunkHandler.wrapChunk(ChunkHandler.java:80)
03-25 23:31:36.271 15881-15888/com.kingrandesigns.uploadpicturetutorial E/art:     at android.ddm.DdmHandleHeap.handleHPIF(DdmHandleHeap.java:112)
03-25 23:31:36.271 15881-15888/com.kingrandesigns.uploadpicturetutorial E/art:     at android.ddm.DdmHandleHeap.handleChunk(DdmHandleHeap.java:85)
03-25 23:31:36.271 15881-15888/com.kingrandesigns.uploadpicturetutorial E/art:     at org.apache.harmony.dalvik.ddmc.DdmServer.dispatch(DdmServer.java:171)
03-25 23:31:36.271 15881-15888/com.kingrandesigns.uploadpicturetutorial I/art: Exception thrown by dispatcher for 0x48504946

1 个答案:

答案 0 :(得分:1)

仔细看看这段代码:

    if (isChecked) {
        while (isChecked) {
            new Download.execute();
            new Upload.execute();
        }
    } else {
        // The toggle is disabled
    }

当第一次选中该复选框时,您将进入无限循环,该循环不断创建下载和上载的新实例,直到内存不足为止。主线程永远不会进入空闲状态,可以再次检查复选框。

请记住,当AsyncTask被安排在另一个线程上工作时,AsyncTask上的执行会立即返回。当您调用execute时,它不会等待任务完成。