package keijumt.devandroid.common import android.os.AsyncTask import java.util.concurrent.CountDownLatch class Task(val callback: Callback<Int>) : AsyncTask<Void, Void, Int>() { override fun doInBackground(vararg params: Void?): Int { val countDownLatch = CountDownLatch(2) var value1 = 0 var value2 = 0 val squireTask1 = SquareTask(object : Callback<Int> { override fun execute(value: Int) { value1 = value } }, countDownLatch) val squareTask2 = SquareTask(object : Callback<Int> { override fun execute(value: Int) { value2 = value } }, countDownLatch) squireTask1.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 3) squareTask2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 5) countDownLatch.await() return value1 * value2 } override fun onPostExecute(result: Int?) { callback.execute(result!!) } private class SquareTask(val callback: Callback<Int>, val countDownLatch: CountDownLatch) : AsyncTask<Int, Void, Int>() { override fun doInBackground(vararg params: Int?): Int { val value = params.get(0)!! return value * value } override fun onPostExecute(result: Int?) { callback.execute(result!!) countDownLatch.countDown() } } interface Callback<T> { fun execute(value: T) } }