簡體   English   中英

如何確定我的android runnable在輔助線程上啟動?

[英]How can I be sure that my android runnable started on a worker thread?

我正在嘗試學習如何通過使用線程在Android / Java上執行后台任務。 我制作了一個簡單的測試應用程序,它純粹執行了一個新的Thread(new Runnable())並在該runnable內部進行了睡眠。 當它運行時,我仍然能夠單擊UI上的按鈕(我可以看到視覺反饋)。

很高興這能正常工作,我將相同的代碼移到了我的主應用程序代碼庫中,但是當GUI鎖定並且睡眠循環完成后彈出ANR時,它似乎仍在UI線程上運行。

有人可以幫我弄清楚為什么我的backgroundThread沒有在Background或Worker線程上運行嗎?

LoginService.java

package com.example.services;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

public class LoginService implements ILoginService {

private Handler handler;

public LoginService() {
    handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg) {
        // Handle a message from the background task
            Log.d("LoginService","Received a Message");
        }
    };
}

/*
    Check if the username exists in the list
 */
@Override
public boolean isUsernameValid(String username) {
    Log.d("LoginService","Starting Runnable");
    Thread backgroundThread = new Thread(new LoginRunnable(this));

    backgroundThread.run();
    Log.d("LoginService", "Started Runnable");
    return true;
}

/*
    Check if the password is correct for the downloaded user
 */
@Override
public boolean doesPasswordMatch(String password) {
    return false;
}

@Override
public void handleStatus(int i) {
    switch (i) {
        case 1:
            Message newMessage = handler.obtainMessage();
            newMessage.sendToTarget();
            break;
        default:
            break;
    }
}
}

LoginRunnable.java

package com.example.services;

import android.util.Log;

public class LoginRunnable implements Runnable {

public ILoginService loginService;

public LoginRunnable(ILoginService loginService)
{
    this.loginService = loginService;
}

@Override
public void run() {
    // Do the login tasks here
    try {
        for(int i = 0;i<=50;i++) {
            Thread.sleep(200);
            Log.d("LoginRunnable","200ms has passed");
            loginService.handleStatus(1);
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    // Return some completion message to the LoginService.

}
}

應用輸出

11-08 18:06:36.757  11722-11722/com.example  D/LoginService﹕ Starting Runnable
11-08 18:06:36.971  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:37.181  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:37.391  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:37.601  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:37.811  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:38.021  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:38.231  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:38.441  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:38.652  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:38.861  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:39.072  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:39.282  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:39.492  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:39.701  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:39.912  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:40.121  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:40.332  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:40.542  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:40.752  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:40.962  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:41.172  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:41.381  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:41.592  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:41.802  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:42.012  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:42.222  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:42.432  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:42.642  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:42.851  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:43.062  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:43.272  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:43.482  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:43.692  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:43.902  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:44.112  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:44.322  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:44.532  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:44.741  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:44.952  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:45.162  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:45.372  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:45.582  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:45.792  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:46.002  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:46.212  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:46.422  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:46.632  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:46.842  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:47.052  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:47.262  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:47.472  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:47.472  11722-11722/com.example  D/LoginService﹕ Started Runnable
11-08 18:06:47.482  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.482  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.483  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.483  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.483  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.483  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.483  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.483  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.483  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.483  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.483  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.487  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.487  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.487  11722-11722/com.example  D/LoginService﹕ Received a Message

工作測試程序(獨立項目)的輸出

11-08 17:57:31.284  32596-32610/com.subarudev.threadingtest D/SleepRunnable﹕ Sleeping on thread
11-08 17:57:31.284  32596-32596/com.subarudev.threadingtest D/MainActivity﹕ Handle Message
11-08 17:57:31.493  32596-32610/com.subarudev.threadingtest D/SleepRunnable﹕ Sleeping on thread
11-08 17:57:31.494  32596-32596/com.subarudev.threadingtest D/MainActivity﹕ Handle Message
11-08 17:57:31.704  32596-32610/com.subarudev.threadingtest D/SleepRunnable﹕ Sleeping on thread
11-08 17:57:31.704  32596-32596/com.subarudev.threadingtest D/MainActivity﹕ Handle Message
11-08 17:57:31.914  32596-32610/com.subarudev.threadingtest D/SleepRunnable﹕ Sleeping on thread
11-08 17:57:31.914  32596-32596/com.subarudev.threadingtest D/MainActivity﹕ Handle Message
11-08 17:57:32.123  32596-32610/com.subarudev.threadingtest D/SleepRunnable﹕ Sleeping on thread
11-08 17:57:32.124  32596-32596/com.subarudev.threadingtest D/MainActivity﹕ Handle Message
11-08 17:57:32.334  32596-32610/com.subarudev.threadingtest D/SleepRunnable﹕ Sleeping on thread
11-08 17:57:32.334  32596-32596/com.subarudev.threadingtest D/MainActivity﹕ Handle Message

backgroundThread.run()-> backgroundThread.start()?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM