假设一个活动A有一些静态成员变量,因为当类首次由VM加载并保留在内存时,静态加载到内存中,我想知道如果其他类B持有A类的任何静态变量,那么对于垃圾收集会发生什么. .?它会被收集吗?是否会收集并将静态初始化为初始值?
java android garbage-collection static-members android-activity
我需要明白,如果我在一个系统中有一个 IO 线程,我在多个协程上运行多个 IO 操作,这些协程是否可以以挂起的方式使用同一个线程(意味着当协程 A 等待 IO 结果时,协程B 可以利用该线程进行 IO 操作)还是线程会被第一个 IO 操作阻塞?
我正在创建一个只有一个线程的线程池执行器,并在 Kotlin 程序中使用 Kotlin 的 asCoroutineDispatcher() 方法。当我从循环中启动多个协程并记录线程名称时,我看到不同的名称 - pool1-thread1、pool3-thread1、pool9-thread-1 等。为什么当我使用单线程作为池时会有多个线程?Kotlin 是否以不同的方式管理线程池?
// this is executed in loop
fun executeTask(url: String) {
GlobalScope.launch {
val result = runAsync(url)
Log.d("coroutineCheck", "$url\t\tStatus:$result")
}
}
//some blocking n/w IO goes in this method
//I log the thread name here
suspend fun runAsync(url: String): String = withContext(Executors.newFixedThreadPool(1).asCoroutineDispatcher()) {
}
Run Code Online (Sandbox Code Playgroud)