본문 바로가기

나의 플랫폼/안드로이드

AsyncTask를 사용하는데 동기화가 안되는 현상.

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

참고 : http://trend21c.tistory.com/1715


위 사이트를 들어가면 제대로 정의 되어 있다.


삽질중에 중요한 안드로이드의 API변화를 발견하게 되었습니다.


When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. After HONEYCOMB, it is planned to change this back to a single thread to avoid common application errors caused by parallel execution. If you truly want parallel execution, you can use the executeOnExecutor(Executor, Params...) version of this method with THREAD_POOL_EXECUTOR; however, see commentary there for warnings on its use.


해석하면


AsyncTask가 처음 도입되었을때는 순차적으로 하나의 백그라운드 쓰레드를 실행하였으나, DONUT(1.6버전)부터는 여러개의 태스크를 병렬적으로 처리하도록 변경이 되었고, 다시 HONEYCOMB(3.0버전)부터는 하나의 쓰레드만 실행하게끔 돌아왔다. 그리고 THREAD_POOL_EXECUTOR를 사용한 executeOnExecutor(Executor, Params...) 로 쓰레드를 병렬적으로 실행시킬수 있게 했다고....



이런 API변화를 놓치고 있었다니....



그러니깐 허니콤이상에서는 execute() 명령어로는 순차적으로 하나의 백그라운드 쓰레드를 실행하는 asynctask였던것입니다.


으아아아아아아니!



이런식으로

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
  myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
  myTask.execute();
}


병렬적으로 asynctask가 동작하도록 할수 있는것이죠.



저두 AsyncTask를 사용했는데... 한 Task가 끝나기 전까지 호출이 되지 않아... 반나절을 허비 했네요;;;;

멀티 쓰레드 구현에서 위 내용은 절대 잊어버리면 안되겠네요. ㅎㄷㄷ..