加载复杂布局,与请求网络数据都是属于特别耗时的事情,当然第一步我希望两个操作都要异步执行;第二:既然两个操作是异步执行的,对于线程执行器要么可以串行处理,要么是并行处理,但是对于多核CPU设备,我希望不能浪费多核CPU的资源,希望能够都能利用起来,对于多核设备,真正并发的线程数(核心线程数)就是CPU数量
要做到加载复杂布局,可以异步加载,同时并行请求网络数据,代码如下:
class InflateUtil(context: Context){
private val contextWrapper = MutableContextWrapper(context)
companion object{
private val CPU_COUNT = Runtime.getRuntime().availableProcessors()
private val CORE_POOL_SIZE = 2.coerceAtLeast((CPU_COUNT - 1).coerceAtMost(4))
private val sDefaultParallelExecutor = Executors.newFixedThreadPool(
CORE_POOL_SIZE
) { r ->
val thread = Thread(r, "NonUiExecutor")
thread.priority = 4
thread
}
}
private val reentrantLock by lazy {
ReentrantLock()
}
private val viewInflated by lazy {
reentrantLock.newCondition()
}
private val dataRequested by lazy {
reentrantLock.newCondition()
}
private val items: Array<Any?> = arrayOfNulls(2)
private val mainHandler = Handler(Looper.getMainLooper()) { msg ->
val request = msg.obj as InflateRequest
request.icallback?.invoke(request.iview!!, request.iresult!!)
true
}
private fun inflateView(request: InflateRequest):FutureTask<View>{
if(request.iresid==null){
throw NullPointerException("resid is null")
}
val workRunable = Callable<View> { BasicInflater(contextWrapper).inflate(request.iresid!!, request.iparent, false) }
return object : FutureTask<View>(workRunable){
override fun done() {
enquee(get(), request)
}
}
}
private fun loadData(request: InflateRequest):FutureTask<String>{
val workRunable = Callable<String> {
val resultt = HttpClientUtils.ayncGetRequest(request.irequestUrl)
resultt
}
return object : FutureTask<String>(workRunable){
override fun done() {
enquee(get(), request)
}
}
}
fun asyncLayoutAndData(@LayoutRes resid: Int, @Nullable parent: ViewGroup, requestUrl: String, callback: (View, String) -> Unit){
InflateRequest().apply {
icallback = callback
iresid = resid
iparent = parent
irequestUrl = requestUrl
}.let {
sDefaultParallelExecutor.execute(inflateView(it))
sDefaultParallelExecutor.execute(loadData(it))
}
}
private fun enquee(objects: Any, request: InflateRequest){
reentrantLock.lock()
val innerItems = items
if(objects is View){
innerItems[0] = objects
}else{
innerItems[1] = objects
}
try {
while (innerItems[0]==null){
viewInflated.await()
}
while (innerItems[1]==null){
dataRequested.await()
}
Message.obtain(mainHandler, 0, request.apply {
iresult = innerItems[1] as String
iview = innerItems[0] as View
}).sendToTarget()
}finally {
reentrantLock.unlock()
}
}
fun attachBaseContext(context: Context){
contextWrapper.baseContext = context
}
private class BasicInflater internal constructor(context: Context?) : LayoutInflater(context) {
override fun cloneInContext(newContext: Context): LayoutInflater {
return BasicInflater(newContext)
}
override fun onCreateView(name: String, attrs: AttributeSet): View {
val var3 = sClassPrefixList
val var4 = var3.size
for (var5 in 0 until var4) {
val prefix = var3[var5]
try {
val view = createView(name, prefix, attrs)
if (view != null) {
return view
}
} catch (var8: ClassNotFoundException) {
}
}
return super.onCreateView(name, attrs)
}
companion object {
private val sClassPrefixList = arrayOf("android.widget.", "android.webkit.", "android.app.")
}
}
private class InflateRequest internal constructor() {
var iresult:String?=null
var iview: View? = null
var icallback: ((View, String) -> Unit)? = null
var iresid: Int?=null
var iparent: ViewGroup?=null
var irequestUrl: String?=null
}
}
调用如下:
InflateUtil(context!!).asyncLayoutAndData(R.layout.setting_layout,contentLayout,"http://baidu.com"){view,result->
contentLayout.addView(view)
Toast.makeText(context!!, "结果${result}", Toast.LENGTH_SHORT).show()
}
其实以上代码参考阻塞队列与AsyncInflate的逻辑写的,大家有更好的思路,望可以讨论讨论