0

我有一个非常简单的应用程序,主要威胁启动 2 个 AsyncTasks。2 个 AsyncTask 几乎相同,这是我的一个 AsyncTask 的代码:

private class waitPlayer extends AsyncTask<Integer, Integer, Void> {

        @Override
        protected Void doInBackground(Integer... params) {

            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                       CreateLog.addToLog(e.toString);
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // Update your layout here
            super.onPostExecute(result);
            setCanPlay(true, lastPlayerPlayed);
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {

             //progress_p1.setProgress((int) progress[0]);
             //progress_p2.setProgress((int) progress[0]);

        }
    }

AsyncTask 唯一要做的就是等待 200 毫秒,然后在完成后调用一个方法。该方法将再次调用 AsyncTask。其他的 AsyncTask 也差不多,只是等待 3000ms,然后调用另一个方法。

问题是,在我添加第二个 AsyncTask 之后,我的应用程序变得非常慢。当 AsyncTasks 运行时,它会冻结几秒钟。我的 Logcat 说充满了垃圾收集器通知。

我的手机没有冻结,只有应用程序。我有大约 200mb 的空闲 RAM。

01-06 19:26:00.695: W/ActivityThread(25739): Application com.obattech.twoplayergame is waiting for the debugger on port 8100...
01-06 19:26:00.705: I/System.out(25739): Sending WAIT chunk
01-06 19:26:00.710: I/dalvikvm(25739): Debugger is active
01-06 19:26:00.905: I/System.out(25739): Debugger has connected
01-06 19:26:00.905: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:01.105: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:01.305: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:01.505: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:01.705: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:01.910: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:02.110: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:02.310: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:02.510: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:02.710: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:02.910: I/System.out(25739): debugger has settled (1351)
01-06 19:26:03.095: D/libEGL(25739): loaded /system/lib/egl/libEGL_mali.so
01-06 19:26:03.095: D/libEGL(25739): loaded /system/lib/egl/libGLESv1_CM_mali.so
01-06 19:26:03.100: D/libEGL(25739): loaded /system/lib/egl/libGLESv2_mali.so
01-06 19:26:03.100: D/(25739): Device driver API match
01-06 19:26:03.100: D/(25739): Device driver API version: 10
01-06 19:26:03.100: D/(25739): User space API version: 10 
01-06 19:26:03.100: D/(25739): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
01-06 19:26:03.135: D/OpenGLRenderer(25739): Enabling debug mode 0
01-06 19:26:05.680: D/AbsListView(25739): Get MotionRecognitionManager
01-06 19:26:09.415: D/dalvikvm(25739): GC_CONCURRENT freed 92K, 5% free 12473K/13127K, paused 22ms+7ms, total 67ms
01-06 19:26:09.425: D/AbsListView(25739): [unregisterDoubleTapMotionListener]
01-06 19:26:09.425: I/MotionRecognitionManager(25739):   .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@42bfb580
01-06 19:26:12.515: D/dalvikvm(25739): GC_CONCURRENT freed 120K, 6% free 12813K/13575K, paused 3ms+2ms, total 21ms
01-06 19:26:12.515: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 14ms
01-06 19:26:13.665: D/dalvikvm(25739): GC_FOR_ALLOC freed 183K, 7% free 13024K/13895K, paused 17ms, total 18ms
01-06 19:26:15.205: D/dalvikvm(25739): GC_CONCURRENT freed 535K, 9% free 12937K/14151K, paused 2ms+3ms, total 19ms
01-06 19:26:15.205: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 16ms
01-06 19:26:17.095: D/dalvikvm(25739): GC_CONCURRENT freed 536K, 9% free 12904K/14151K, paused 4ms+2ms, total 25ms
01-06 19:26:17.095: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 7ms
01-06 19:26:17.160: D/dalvikvm(25739): GC_CONCURRENT freed 445K, 9% free 12893K/14151K, paused 12ms+12ms, total 35ms
01-06 19:26:19.765: D/dalvikvm(25739): GC_CONCURRENT freed 196K, 7% free 13201K/14151K, paused 5ms+5ms, total 36ms
01-06 19:26:19.765: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 13ms
01-06 19:26:23.260: D/dalvikvm(25739): GC_FOR_ALLOC freed 524K, 9% free 13025K/14279K, paused 18ms, total 18ms
01-06 19:26:26.350: D/dalvikvm(25739): GC_CONCURRENT freed 536K, 10% free 12992K/14279K, paused 3ms+2ms, total 22ms
01-06 19:26:26.350: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 5ms
01-06 19:26:28.290: D/dalvikvm(25739): GC_CONCURRENT freed 548K, 10% free 12948K/14279K, paused 6ms+2ms, total 26ms
01-06 19:26:28.290: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 8ms
01-06 19:26:29.445: D/dalvikvm(25739): GC_CONCURRENT freed 548K, 10% free 12904K/14279K, paused 7ms+2ms, total 30ms
01-06 19:26:29.445: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 7ms
01-06 19:26:29.510: D/dalvikvm(25739): GC_CONCURRENT freed 137K, 8% free 13201K/14279K, paused 3ms+13ms, total 36ms
01-06 19:26:32.065: D/dalvikvm(25739): GC_FOR_ALLOC freed 480K, 9% free 13068K/14279K, paused 17ms, total 17ms
01-06 19:26:36.005: D/dalvikvm(25739): GC_CONCURRENT freed 536K, 9% free 13036K/14279K, paused 14ms+4ms, total 49ms
01-06 19:26:36.005: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 12ms
01-06 19:26:38.665: D/dalvikvm(25739): GC_CONCURRENT freed 547K, 10% free 12992K/14279K, paused 15ms+4ms, total 48ms
01-06 19:26:38.665: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 15ms
01-06 19:26:40.605: D/dalvikvm(25739): GC_CONCURRENT freed 548K, 10% free 12948K/14279K, paused 13ms+2ms, total 32ms
01-06 19:26:40.605: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 6ms
01-06 19:26:43.160: D/dalvikvm(25739): GC_CONCURRENT freed 548K, 10% free 12904K/14279K, paused 13ms+2ms, total 34ms
01-06 19:26:43.160: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 4ms
01-06 19:26:43.225: D/dalvikvm(25739): GC_CONCURRENT freed 533K, 11% free 12805K/14279K, paused 2ms+13ms, total 32ms
01-06 19:26:46.360: D/dalvikvm(25739): GC_CONCURRENT freed 108K, 8% free 13201K/14279K, paused 3ms+2ms, total 22ms
01-06 19:26:46.360: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 8ms
01-06 19:26:47.980: D/dalvikvm(25739): GC_CONCURRENT freed 524K, 9% free 13080K/14279K, paused 12ms+2ms, total 34ms
01-06 19:26:47.980: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 12ms
01-06 19:26:50.405: D/dalvikvm(25739): GC_CONCURRENT freed 547K, 9% free 13036K/14279K, paused 2ms+3ms, total 21ms
01-06 19:26:50.405: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 8ms
01-06 19:26:53.925: D/dalvikvm(25739): GC_CONCURRENT freed 548K, 10% free 12992K/14279K, paused 4ms+4ms, total 38ms
01-06 19:26:53.925: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 18ms
01-06 19:26:55.260: D/dalvikvm(25739): GC_CONCURRENT freed 548K, 10% free 12948K/14279K, paused 8ms+2ms, total 36ms
01-06 19:26:55.260: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 10ms
01-06 19:26:57.080: D/dalvikvm(25739): GC_CONCURRENT freed 548K, 10% free 12904K/14279K, paused 22ms+2ms, total 46ms
01-06 19:26:57.080: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 10ms
01-06 19:26:57.135: D/dalvikvm(25739): GC_CONCURRENT freed 489K, 11% free 12849K/14279K, paused 2ms+12ms, total 29ms
01-06 19:26:58.420: D/dalvikvm(25739): GC_CONCURRENT freed 152K, 8% free 13201K/14279K, paused 13ms+2ms, total 33ms
01-06 19:26:58.420: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 6ms
01-06 19:26:59.565: D/dalvikvm(25739): GC_FOR_ALLOC freed 524K, 9% free 13025K/14279K, paused 18ms, total 19ms
01-06 19:27:02.100: D/dalvikvm(25739): GC_CONCURRENT freed 536K, 10% free 12992K/14279K, paused 2ms+2ms, total 22ms
01-06 19:27:02.100: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 8ms
01-06 19:27:03.685: D/dalvikvm(25739): GC_CONCURRENT freed 547K, 10% free 12948K/14279K, paused 7ms+3ms, total 29ms
01-06 19:27:03.685: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 8ms
01-06 19:27:06.535: D/dalvikvm(25739): GC_CONCURRENT freed 548K, 10% free 12904K/14279K, paused 18ms+3ms, total 52ms
01-06 19:27:06.535: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 9ms
01-06 19:27:06.580: D/dalvikvm(25739): GC_CONCURRENT freed 489K, 11% free 12849K/14279K, paused 2ms+2ms, total 18ms
01-06 19:27:08.065: D/dalvikvm(25739): GC_CONCURRENT freed 152K, 8% free 13201K/14279K, paused 3ms+1ms, total 41ms
01-06 19:27:08.065: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 19ms
01-06 19:27:11.150: D/dalvikvm(25739): GC_FOR_ALLOC freed 526K, 9% free 13025K/14279K, paused 17ms, total 17ms
01-06 19:27:12.600: D/dalvikvm(25739): GC_CONCURRENT freed 545K, 10% free 12982K/14279K, paused 4ms+5ms, total 52ms
01-06 19:27:12.600: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 26ms
01-06 19:27:15.065: D/dalvikvm(25739): GC_CONCURRENT freed 550K, 10% free 12938K/14279K, paused 12ms+3ms, total 37ms
01-06 19:27:15.065: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 6ms
01-06 19:27:15.125: D/dalvikvm(25739): GC_CONCURRENT freed 521K, 11% free 12805K/14279K, paused 3ms+12ms, total 32ms
01-06 19:27:19.575: D/dalvikvm(25739): GC_CONCURRENT freed 110K, 8% free 13201K/14279K, paused 12ms+3ms, total 41ms
01-06 19:27:19.575: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 4ms
01-06 19:27:29.260: D/dalvikvm(25739): GC_FOR_ALLOC freed 534K, 9% free 13028K/14279K, paused 15ms, total 19ms
01-06 19:27:50.315: D/dalvikvm(25739): GC_CONCURRENT freed 546K, 10% free 12957K/14279K, paused 13ms+2ms, total 39ms
01-06 19:27:50.315: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 9ms
01-06 19:27:55.140: D/dalvikvm(25739): GC_CONCURRENT freed 537K, 11% free 12803K/14279K, paused 17ms+3ms, total 36ms
01-06 19:28:05.150: W/jdwp(25739): Debugger is telling the VM to exit with code=1
01-06 19:28:05.150: I/dalvikvm(25739): GC lifetime allocation: 42530 bytes
01-06 19:28:05.510: D/libEGL(27466): loaded /system/lib/egl/libEGL_mali.so
01-06 19:28:05.520: D/libEGL(27466): loaded /system/lib/egl/libGLESv1_CM_mali.so
01-06 19:28:05.520: D/libEGL(27466): loaded /system/lib/egl/libGLESv2_mali.so
01-06 19:28:05.525: D/(27466): Device driver API match
01-06 19:28:05.525: D/(27466): Device driver API version: 10
01-06 19:28:05.525: D/(27466): User space API version: 10 
01-06 19:28:05.525: D/(27466): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
01-06 19:28:05.550: D/OpenGLRenderer(27466): Enabling debug mode 0
01-06 19:28:19.100: D/libEGL(27760): loaded /system/lib/egl/libEGL_mali.so
01-06 19:28:19.105: D/libEGL(27760): loaded /system/lib/egl/libGLESv1_CM_mali.so
01-06 19:28:19.110: D/libEGL(27760): loaded /system/lib/egl/libGLESv2_mali.so
01-06 19:28:19.110: D/(27760): Device driver API match
01-06 19:28:19.110: D/(27760): Device driver API version: 10
01-06 19:28:19.110: D/(27760): User space API version: 10 
01-06 19:28:19.110: D/(27760): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
01-06 19:28:19.165: D/OpenGLRenderer(27760): Enabling debug mode 0
01-06 19:28:48.755: D/AbsListView(27760): Get MotionRecognitionManager
01-06 19:28:50.325: D/dalvikvm(27760): GC_CONCURRENT freed 107K, 6% free 12461K/13191K, paused 3ms+13ms, total 31ms
01-06 19:28:50.330: D/AbsListView(27760): [unregisterDoubleTapMotionListener]
01-06 19:28:50.330: I/MotionRecognitionManager(27760):   .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@42bff9d8
01-06 19:28:55.810: D/dalvikvm(27760): GC_CONCURRENT freed 127K, 6% free 12813K/13575K, paused 3ms+3ms, total 22ms
01-06 19:28:55.810: D/dalvikvm(27760): WAIT_FOR_CONCURRENT_GC blocked 16ms
01-06 19:28:56.915: D/dalvikvm(27760): GC_FOR_ALLOC freed 181K, 7% free 13024K/13895K, paused 18ms, total 18ms
01-06 19:28:58.235: D/dalvikvm(27760): GC_CONCURRENT freed 537K, 9% free 12936K/14151K, paused 2ms+2ms, total 14ms
01-06 19:28:58.235: D/dalvikvm(27760): WAIT_FOR_CONCURRENT_GC blocked 10ms
01-06 19:28:59.130: D/dalvikvm(27760): GC_CONCURRENT freed 527K, 10% free 12793K/14151K, paused 2ms+2ms, total 13ms
01-06 19:29:00.920: D/dalvikvm(27760): GC_CONCURRENT freed 104K, 7% free 13200K/14151K, paused 3ms+3ms, total 31ms
01-06 19:29:00.920: D/dalvikvm(27760): WAIT_FOR_CONCURRENT_GC blocked 13ms
01-06 19:29:06.065: D/dalvikvm(27760): GC_CONCURRENT freed 536K, 9% free 13068K/14279K, paused 5ms+4ms, total 36ms
01-06 19:29:06.065: D/dalvikvm(27760): WAIT_FOR_CONCURRENT_GC blocked 30ms
01-06 19:29:16.625: D/dalvikvm(27760): GC_CONCURRENT freed 547K, 9% free 13027K/14279K, paused 14ms+4ms, total 50ms
01-06 19:29:16.625: D/dalvikvm(27760): WAIT_FOR_CONCURRENT_GC blocked 16ms
01-06 19:29:36.525: D/dalvikvm(27760): GC_CONCURRENT freed 540K, 10% free 12988K/14279K, paused 15ms+4ms, total 56ms
01-06 19:29:36.525: D/dalvikvm(27760): WAIT_FOR_CONCURRENT_GC blocked 21ms
01-06 19:30:06.085: W/IInputConnectionWrapper(27760): showStatusIcon on inactive InputConnection
01-06 19:30:13.920: D/dalvikvm(27760): GC_CONCURRENT freed 549K, 10% free 12926K/14279K, paused 20ms+8ms, total 100ms
01-06 19:30:13.920: D/dalvikvm(27760): WAIT_FOR_CONCURRENT_GC blocked 50ms
01-06 19:30:13.995: D/dalvikvm(27760): GC_CONCURRENT freed 137K, 8% free 13223K/14279K, paused 13ms+3ms, total 44ms
4

3 回答 3

7

AsyncTask 有一个巨大的愚蠢。引用开发文档:

“首次引入时,AsyncTasks 在单个后台线程上串行执行。从 DONUT 开始,这被更改为允许多个任务并行运行的线程池。从 HONEYCOMB 开始,任务在单个线程上执行以避免常见的应用程序并行执行导致的错误。

“如果你真的想要并行执行,你可以使用 THREAD_POOL_EXECUTOR 调用 executeOnExecutor(java.util.concurrent.Executor, Object[])。”

这意味着在 AsyncTask::doInBackground 中休眠将阻止所有其他 AsyncTask 运行,直到休眠完成。您很可能会阻止其他后台活动(可能是系统库活动)完成。

于 2013-01-06T20:02:18.577 回答
1

如果您希望在不久的将来做某事,您可以使用处理程序计时器

例子 :

这将在大约 2 秒内运行 runnable:

new Handler().postDelayed(new Runnable() {...} , 2000);

asyncTask 仅用于小任务,您不关心它们何时完成的顺序,也不关心有多少线程正在处理它们。

例如,它们可用于从 Internet 下载小图像,如果在滚动速度过快的列表视图中使用它们,它们将被取消。

无论如何,我们需要看更多你的代码:什么是“setCanPlay”?对另一个 asyncTask 的调用在哪里?

于 2013-01-06T20:09:40.510 回答
0

我发现问题出在 => AsyncTask 类使用串行通道在新版本的 android 上编译您的应用程序,例如 jellybean 版本 => 4.2

与 Eclair 版本 => 2.0 等旧版本不同,它使用并行通道来编译 AsyncTask 所以如果你像我所做的那样在旧版本上尝试你的应用程序,你会看到不同的 你在这里看到的不同

如果你想解决这个问题,这将帮助你:-

调用executeOnExecutor(java.util.concurrent.Executor, Object[])

使用THREAD_POOL_EXECUTOR。"

于 2018-08-29T12:15:13.507 回答