作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
*所以我有一个包含 sleep 方法的线程,我认为这是问题所在,因为当我尝试使用 stop() 方法停止它时我的程序崩溃了。当用户点击后退按钮(硬件后退按钮)时,我试图完全停止线程。我该怎么办?我应该使用 sleep 方法以外的方法吗? *
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
最佳答案
您不应该在可运行对象上调用 stop()。相反,您需要让 Thread 返回。为此,您只需要一个可以设置为 true 的标志。在这种情况下,您将 mStopThread 设置为 false,当您可以终止线程时,只需将其设置为 true,它就会返回。但是,我通常会在 sleep 时间较短的某种循环中看到这一点。
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() 然后让它返回来唤醒您的线程更紧密的循环。
关于android - 如何在 Android 开发中停止线程中具有 sleep 方法的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11127444/
我是一名优秀的程序员,十分优秀!