首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Thread.Interrupt()在Android中不像预期的那样工作?

Thread.Interrupt()在Android中不像预期的那样工作?
EN

Stack Overflow用户
提问于 2018-08-09 22:44:04
回答 1查看 155关注 0票数 0

这是我的密码:

调用中断确实会停止线程,但只需一秒,线程就会继续,但是为什么呢?,我目前解决这个问题的解决方案是使用ArrayList管理线程,并在ArrayList上循环以停止线程。虽然这样做有效,但肯定不是很好。有人能帮我吗?

代码语言:javascript
复制
private Thread thread;
private static final String TAG = "StoragePresenter";

public void refreshVolumes(boolean isRunning) {
    if (isRunning) {
        Handler handler = new Handler();
        Runnable runnable = () -> {
            while (!thread.isInterrupted()) {
                Log.d(TAG, "refreshVolumes: thread.isInterrupted? above while " + thread.isInterrupted());
                try {
                    Thread.sleep(refreshDelay);
                    Log.d(TAG, "refreshVolumes: in TRY ---> " + Thread.currentThread().getName()); //Thread -> 9
                } catch (InterruptedException ignored) {
                }
                Log.d(TAG, "refreshVolumes: in runnable WHILE ---> " + Thread.currentThread().getName());
                handler.post(this::getVolumes);
            }
        };
        thread = new Thread(runnable);
        Log.d(TAG, "refreshVolumes: start thread's name -> " + thread.getName());
        thread.start(); // Thread -> 9 started
    } else {
        Log.d(TAG, "refreshVolumes: interrupt thread's name -> " + thread.getName());
        thread.interrupt(); // Thread -> 9 interrupted
        Log.d(TAG, "refreshVolumes: thread.isInterrupted? " + thread.isInterrupted());
        Log.d(TAG, "refreshVolumes: thread.isInterrupted? 2x " + thread.isInterrupted());
    }
}

Logcat输出:

代码语言:javascript
复制
        ////////////////////////////////////
        called refreshVolumes(true)
        ////////////////////////////////////
        08-09 17:41:29.687 924-1811/? I/ActivityManager: START u0 {cmp=com.xxxxxxxxx.android.dev/com.xxxxxxxxx.app.ui.main.settings.storage.StorageActivity} from uid 10293
        08-09 17:41:29.767 26451-26451/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: start thread's name -> Thread-9
        08-09 17:41:29.768 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: thread.isInterrupted? above while false
        08-09 17:41:29.927 924-995/? I/ActivityManager: Displayed com.xxxxxxxxx.android.dev/com.xxxxxxxxx.app.ui.main.settings.storage.StorageActivity: +210ms
        08-09 17:41:33.269 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: in TRY ---> Thread-9
            refreshVolumes: in runnable WHILE ---> Thread-9
        08-09 17:41:33.271 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: thread.isInterrupted? above while false
        08-09 17:41:36.772 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: in TRY ---> Thread-9
            refreshVolumes: in runnable WHILE ---> Thread-9
        08-09 17:41:36.773 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: thread.isInterrupted? above while false
        08-09 17:41:40.274 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: in TRY ---> Thread-9
            refreshVolumes: in runnable WHILE ---> Thread-9
        08-09 17:41:40.275 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: thread.isInterrupted? above while false
        08-09 17:41:43.776 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: in TRY ---> Thread-9
            refreshVolumes: in runnable WHILE ---> Thread-9
        08-09 17:41:43.777 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: thread.isInterrupted? above while false
        08-09 17:41:47.278 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: in TRY ---> Thread-9
        08-09 17:41:47.279 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: in runnable WHILE ---> Thread-9
        08-09 17:41:47.280 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: thread.isInterrupted? above while false
        08-09 17:41:50.780 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: in TRY ---> Thread-9
            refreshVolumes: in runnable WHILE ---> Thread-9
            refreshVolumes: thread.isInterrupted? above while false
        ////////////////////////////////////
        called refreshVolumes(false)
        ////////////////////////////////////
        08-09 17:41:50.861 26451-26451/com.xxxxxxxxx.android.dev
D/StoragePresenter: refreshVolumes: interrupt thread's name -> Thread-9
        08-09 17:41:50.862 26451-26451/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: thread.isInterrupted? true
            refreshVolumes: thread.isInterrupted? 2x false
        08-09 17:41:50.862 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: in runnable WHILE ---> Thread-9
            refreshVolumes: thread.isInterrupted? above while false
        08-09 17:41:54.362 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: in TRY ---> Thread-9
        08-09 17:41:54.363 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: in runnable WHILE ---> Thread-9
        08-09 17:41:54.376 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: thread.isInterrupted? above while false
        08-09 17:41:57.878 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: in TRY ---> Thread-9
            refreshVolumes: in runnable WHILE ---> Thread-9
        08-09 17:41:57.879 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: thread.isInterrupted? above while false
        08-09 17:42:01.380 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: in TRY ---> Thread-9
            refreshVolumes: in runnable WHILE ---> Thread-9
        08-09 17:42:01.381 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: thread.isInterrupted? above while false
        08-09 17:42:04.882 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: in TRY ---> Thread-9
            refreshVolumes: in runnable WHILE ---> Thread-9
        08-09 17:42:04.883 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: thread.isInterrupted? above while false
        08-09 17:42:08.384 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: in TRY ---> Thread-9
        08-09 17:42:08.385 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: in runnable WHILE ---> Thread-9
            refreshVolumes: thread.isInterrupted? above while false
        08-09 17:42:11.886 26451-26676/com.xxxxxxxxx.android.dev D/StoragePresenter: refreshVolumes: in TRY ---> Thread-9
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-09 23:51:39

Thread.sleep抛出InterruptedException (由于睡觉时被中断)时,它会清除中断标志。如果您希望InterruptedException以这种方式工作,您应该实际处理它,并使用catch块来脱离循环。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51776693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档