1、伴生对象的引用
2、Kotlin 的尾递归
左子树遍历
internal class CombinedContext(
private val left: CoroutineContext,
private val element: Element
) : CoroutineContext, Serializable {
override fun <E : Element> get(key: Key<E>): E? {
var cur = this
while (true) {
cur.element[key]?.let { return it }
val next = cur.left
if (next is CombinedContext) {
cur = next
} else {
return next[key]
}
}
}
3、并发问题
如非必要不要随意切换线程
GlobalScope.launch {
var num = 0
Executors.newFixedThreadPool(10).asCoroutineDispatcher().use { executorCoroutineDispatcher ->
List(1000000) {
GlobalScope.launch(executorCoroutineDispatcher) {
num++;
}
}.forEach {
it.join()
}
}
print(num)
}
Thread.sleep(1000)
> Task :Main.main()
999995
协程方法另一种写法
全局异常处理
String[] PermissionString={Manifest.permission.READ_EXTERNAL_STORAGE};
ActivityCompat.requestPermissions(SettingGuideActivity.this,
PermissionString, 1);