Created
July 9, 2019 12:35
-
-
Save adityawibisana/5db95ffbe6116f519ac393b42ec821e1 to your computer and use it in GitHub Desktop.
Run on UI and background easily
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.os.Handler | |
import android.os.HandlerThread | |
import android.os.Looper | |
import android.util.Log | |
object CommonUtils { | |
private const val TAG = "CommonUtils" | |
private val handler: Handler by lazy { | |
val handlerThread = HandlerThread("DefaultNormalThread", Thread.NORM_PRIORITY) | |
handlerThread.start() | |
Handler(handlerThread.looper) | |
} | |
private const val WHAT_IS_RUNNING : Int = 1 | |
private val handlers: ArrayList<Handler> by lazy { | |
val handlerThreads = arrayListOf<HandlerThread>() | |
val handlers = arrayListOf<Handler>() | |
for (i in 0..12) { | |
handlerThreads.add(HandlerThread("", Thread.MIN_PRIORITY)) | |
handlerThreads[i].start() | |
handlers.add(Handler(handlerThreads[i].looper)) | |
} | |
handlers | |
} | |
fun runOnDefaultThread(runnable: () -> Unit, delayMillis: Long = 0) { | |
handler.postDelayed(runnable, delayMillis) | |
} | |
fun runOnUIThread(runnable: () -> Unit, delayMillis: Long = 0) { | |
Handler(Looper.getMainLooper()).postDelayed(runnable, delayMillis) | |
} | |
//TODO: close after thread has been fully finished | |
fun runOnUnusedThread (runnable: () -> Unit) { | |
handlers[12].post { | |
do { | |
val filtered = handlers.indexOfFirst { | |
!it.hasMessages(WHAT_IS_RUNNING) && it != handlers[12] | |
} | |
if (filtered != -1) { | |
Log.v(TAG, "Using index:$filtered for the thread") | |
handlers[filtered].post { | |
handlers[filtered].sendEmptyMessage(WHAT_IS_RUNNING) | |
runnable() | |
handlers[filtered].removeMessages(WHAT_IS_RUNNING) | |
} | |
break | |
} | |
Log.v(TAG, "All thread is busy. Retrying...") | |
} while (filtered == -1) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment