1

*所以我有一个拥有 sleep 方法的线程,我认为这是问题所在,因为当我尝试使用 stop() 方法停止它时,我的程序崩溃了。当用户点击后退按钮(硬件后退按钮)时,我试图完全停止线程。我该怎么办?我应该使用睡眠方法以外的方法吗?*

Thread timer2 = new Thread() { // Threads - do multiple things
        public void run() {
            try {
                sleep(5000); // 5000 mil secs = 5 secs . sleeps thread
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                cdt2.start();
            }
        }
    };
    timer2.start(); // starts thread


public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
          timer1.stop(); //error
          timer2.stop(); //error
          cdt.cancel();
          cdt2.cancel();
          Intent menu = new Intent("android.intent.action.MENU");
          startActivity(menu);
          return true;
        }
        return false;
      }

日志猫:

06-20 13:08:58.006: D/dalvikvm(22050): GC_CONCURRENT freed 951K, 7% free 15393K/16455K,    paused 2ms+3ms
06-20 13:08:58.944: D/dalvikvm(22050): GC_CONCURRENT freed 700K, 6% free 15533K/16455K, paused 2ms+1ms
06-20 13:09:01.733: D/dalvikvm(22050): GC_CONCURRENT freed 955K, 7% free 15630K/16647K, paused 2ms+2ms
06-20 13:09:02.069: D/dalvikvm(22050): GC_CONCURRENT freed 880K, 7% free 15707K/16711K, paused 2ms+2ms
06-20 13:09:02.678: D/dalvikvm(22050): GC_CONCURRENT freed 762K, 5% free 15940K/16775K, paused 2ms+2ms
06-20 13:09:03.303: D/AccountTypeManager(21214): Registering external account type=ro.weednet.contactssync, packageName=ro.weednet.contactssync
06-20 13:09:03.311: D/AccountTypeManager(21214): Registering external account type=com.skype.contacts.sync, packageName=com.skype.raider
06-20 13:09:03.311: W/ResourceType(21214): getEntry failing because entryIndex 1377 is beyond type entryCount 189
06-20 13:09:03.311: W/ResourceType(21214): Failure getting entry for 0x7f020561 (t=1 e=1377) in package 0 (error -2147483647)
06-20 13:09:03.311: W/ResourceType(21214): getEntry failing because entryIndex 1378 is beyond type entryCount 189
06-20 13:09:03.311: W/ResourceType(21214): Failure getting entry for 0x7f020562 (t=1 e=1378) in package 0 (error -2147483647)
06-20 13:09:03.311: W/ResourceType(21214): getEntry failing because entryIndex 1379 is beyond type entryCount 189
06-20 13:09:03.311: W/ResourceType(21214): Failure getting entry for 0x7f020563 (t=1 e=1379) in package 0 (error -2147483647)
06-20 13:09:03.311: W/ResourceType(21214): getEntry failing because entryIndex 1778 is beyond type entryCount 189
06-20 13:09:03.311: W/ResourceType(21214): Failure getting entry for 0x7f0206f2 (t=1 e=1778) in package 0 (error -2147483647)
06-20 13:09:03.311: D/AccountTypeManager(21214): Registering external account type=com.yahoo.mobile.client.share.sync, packageName=com.yahoo.mobile.client.android.mail
06-20 13:09:03.319: D/AccountTypeManager(21214): Registering external account type=com.facebook.auth.login, packageName=com.facebook.katana
06-20 13:09:03.319: W/ResourceType(21214): getEntry failing because entryIndex 344 is beyond type entryCount 189
4

2 回答 2

2

您不应该在可运行文件上调用 stop() 。相反,您需要让 Thread 返回。为此,您只需要一个可以设置为 true 的标志。在这种情况下,您将 mStopThread 设置为 false,当您可以终止线程时,只需将其设置为 true,它就会返回。但是,我通常会在睡眠时间较短的某种循环中看到这一点。

Thread timer2 = new Thread() { // Threads - do multiple things
        public void run() {
            try {
                if (mStopThread) return;
                sleep(5000); // 5000 mil secs = 5 secs . sleeps thread
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                cdt2.start();
            }
        }
    };
timer2.start(); // starts thread

如果您没有选择修改代码示例以使循环更紧密,那么您总是可以通过调用 timer2.interrupt() 来唤醒您的线程,然后让它返回。

于 2012-06-20T20:17:57.360 回答
0

你试过用吗

timer.interrupt()

代替

timer.stop()

这就是现在的“更正确的方式”。然而,真正干净的方法,正如 Frank Sposaro 已经证明的那样,是有一个内部标志,您可以查看线程中可以停止的每个点,然后从内部停止线程。至于 sleep() 场景,试试 interrupt()

于 2012-06-20T20:25:45.603 回答