[Android] AsyncTask

[Android] AsyncTask

AsyncTask

๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—… ๊ฒฐ๊ณผ๋ฅผ UI๋กœ ์ „๋‹ฌ

[์ถœ์ฒ˜: https://androidkennel.org/android-networking-tutorial-with-asynctask/]


public class FullTask extends AsyncTask<Params, Progress, Result> { ... }
  • Params
    ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰๋˜๋Š” ํƒœ์Šคํฌ๋กœ ์ž…๋ ฅ๋˜๋Š” ๋ฐ์ดํ„ฐ

  • Progress
    ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์Šค๋ ˆ๋“œ์˜ doInBackgroud์—์„œ UI์Šค๋ ˆ๋“œ์˜ onProgressUpdate๋กœ ๋ณด๋‚ผ ์ง„ํ–‰ ๋ฐ์ดํ„ฐ

  • Result
    UI ์Šค๋ ˆ๋“œ๋กœ ๋ณด๋‚ด์ง„ ๊ฒฐ๊ณผ

์ƒํƒœ

  • PENDING: task๊ฐ€ ์•„์ง ์‹คํ–‰๋˜์ง€ ์•Š์€ ์ƒํƒœ
  • RUNNING: ์‹คํ–‰์ค‘์ธ ์ƒํƒœ
  • FINISHED: AsyncTask.onPostExecute()๊นŒ์ง€ ๋๋‚œ ์ƒํƒœ

AsyncTask ์ทจ์†Œ

task.cancel(true);
  • true: isCancelled()๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ํ”Œ๋ž˜๊ทธ ์„ค์ • + ์ธํ„ฐ๋ŸฝํŠธ
  • false: isCancelled()๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ํ”Œ๋ž˜๊ทธ ์„ค์ •

์Šค๋ ˆ๋“œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ๊ฐ•์ œ ์ข…๋ฃŒํ•  ์ˆ˜ ์—†๋‹ค.

InterruptedException์„ ๋˜์งˆ ๋•Œ ์ข…๋ฃŒ
๊ธด ์ž‘์—… ์‹œ์ž‘ ์ „ ํ™•์ธํ•˜๋Š” ์ฒดํฌํฌ์ธํŠธ (๋ฐ˜๋ณต๋ฌธ์˜ ์กฐ๊ฑด๋ฌธ์ด๋‚˜ ๊ธด ์ž‘์—… ์‚ฌ์ด)

Flow

Status = PENDING

  1. AsyncTask()

    • Worker ์ƒ์„ฑ
      mWorker = new WorkerRunnable<Params, Result>() {
         public Result More ...call() throws Exception {
             ...
             return postResult(doInBackground(mParams));
             // postResult() : MESSAGE_POST_RESULT ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋ƒ„
         }
     };
    
    • FutureTask ์ƒ์„ฑ
     mFuture = new FutureTask<Result>(mWorker) {
         @Override
         protected void More ...done() {
             try {
                 postResultIfNotInvoked(get());
             } catch (InterruptedException e) {
                 ...
             }
         }
     };
    
  2. .execute()

    Status = RUNNING

    • .onPreExecute() ํ˜ธ์ถœ
    • doInBackground()
     exec.execute(mFuture); // -> doInBackground() ์‹คํ–‰
    
  3. doInBackground()
    • publishProgress() ํ˜ธ์ถœํ•˜๋Š” ๊ฒฝ์šฐ
      MESSAGE_POST_PROGRESS ๋ฉ”์‹œ์ง€ ๋ณด๋ƒ„
      onProgressUpdate(); ์‹คํ–‰

    • doInBackground() ์ข…๋ฃŒ ์‹œ
      MESSAGE_POST_RESULT ๋ฉ”์‹œ์ง€ ๋ณด๋ƒ„
      finish(); ์‹คํ–‰

  4. finish()
    • isCancelled() = true
      onCancelled(); ์‹คํ–‰
    • isCancelled() = false
      onPostExecute(); ์‹คํ–‰

Status = FINISHED

AsyncTask์˜ InternalHandler

private static class More ...InternalHandler extends Handler {
   public More ...InternalHandler() {
       super(Looper.getMainLooper()); // Looper-MainLooper
   }

   @SuppressWarnings({"unchecked", "RawUseOfParameterizedType"})
   @Override
   public void More ...handleMessage(Message msg) {
       AsyncTaskResult<?> result = (AsyncTaskResult<?>) msg.obj;
       switch (msg.what) {
           case MESSAGE_POST_RESULT: // postResult()์—์„œ
               // There is only one result
               result.mTask.finish(result.mData[0]);
              break;
           case MESSAGE_POST_PROGRESS: // publishProgress()์—์„œ
               result.mTask.onProgressUpdate(result.mData);
               break;
       }
   }
}

ThreadPool

Comments