当前位置: 代码网 > it编程>编程语言>Java > 【安卓面试】Kotlin知识点盘点

【安卓面试】Kotlin知识点盘点

2024年07月28日 Java 我要评论
CoroutineScope 的实例可以通过 CoroutineScope() 或 MainScope() 的工厂函数来构建。前者创建通用作用域,后者创建 UI 应用程序的作用域并使用 Dispatchers.Main 作为默认的调度器${Threadname")// 关闭自定义线程池Executors)?

kotlin知识点盘点
资料参考
  1. kotlin委托 + viewbinding
构造函数、init方法的初始化顺序
kotlin 伴生对象
koltin inline 标识的方法
kotlin扩展函数原理
kotlin扩展函数适合使用场景
kotlin 重载 jvmoverloads
  • 因为kotlin支持默认参数,这个注解是为了兼容java的重载的写法
crossinline 关键字
  • 参考
  • 用于禁止 代码块/函数参数体 内部使用了return,因为无法确定这个return,是返回这个函数体还是其他方法(通常会在inline函数)
inline fun maininline2(crossinline func1: () -> unit) {
    runonuithread {
            func1()
        }
}
作用域函数的区别?(let、also、apply、run、with等)
kotlin == 与 === 的区别
kotlin空安全原理
kotlin 委托
重写属性的get或者set方法
var test = "123"
get()
{
	return if (field == "123"){
		"321"
	}else field
}
set(value) {
	field = value + "fix"
}
kotlin协程相关
  • 资料参考
几个常见的类
协程的异常处理:supervisorscope与supervisorscope的创建与使用
// 先创建 supervisor 作用域
// 在该作用域下创建的协程都是 supervisorjob 协程
val supervisorscope = coroutinescope(supervisorjob())

// 通过 supervisor 作用域 创建 协程
val job = supervisorscope.launch {
    delay(100)
    log.i(tag, "子协程 job 执行")
}

globalscope.launch(coroutineexceptionhandler{_,t ->
     log.e(tag,"coroutineexceptionhandler:$t")
 }) {

     supervisorscope {

                launch(coroutineexceptionhandler{_,t ->
                    log.e(tag,"supervisorscope coroutineexceptionhandler:$t")
                }) {
                    delay(3000)
                    throw nullpointerexception()
                }

                launch {
                    delay(4000)
                    log.i(tag, "launch2: 正常执行")
                }

            }

 }

  • 超时取消
// 超时抛出异常,导致crash
withtimeout(1-00l) {
    repeat(1000) { i ->
        println("i'm sleeping $i ...")
        delay(500l)
    }
}
// 超时返回null
val result = withtimeoutornull(1000l) {
    repeat(1000) { i ->
        println("i'm sleeping $i ...")
        delay(500l)
    }
    "done" // will get cancelled before it produces this result
}
println("result is $result")
kotlin协程的挂起和恢复
  • kotlin协程和线程的联系
kotlin取消协程

协程的取消机制

  • kotlin自动取消协程与生命周期绑定
协程的withcontext和async的区别
channel和flow
协程构建器与作用域函数
特性协程构建器协程作用域函数
常用构建器launch、async、producecoroutinescope、supervisorscope、withcontext、withtimeout
是否是 coroutinescope 扩展函数
是否是挂起函数
协程上下文携带来自 coroutinescope挂起函数 continuation
异常传递方式通过 job 传递给父协程与常规函数异常抛出方式相同
启动异步协程是否立即执行可能不会立即执行就地启动
suspendcoroutine与coroutinescope
特性suspendcoroutinecoroutinescope
类型挂起函数挂起函数
目的手动控制挂起和恢复创建协程作用域,管理协程执行
使用场景需要控制异步 api 的挂起点启动并等待协程作用域内的协程完成
参数挂起函数 continuation无参数
阻塞主线程否(不会阻塞主线程)
底层线程资源释放是(协程作用域内释放底层线程资源)
几个常用函数区别
函数创建协程用途和功能返回值阻塞
launch启动一个新的协程,执行后台任务job
async启动一个协程以获取异步操作结果,可以并行执行deferred否(调用了await会阻塞当前协程)
runblocking在当前线程阻塞直到其协程块执行完成
withcontext在指定调度器上执行协程代码块,可以挂起当前协程返回代码块结果
coroutinescope创建一个协程范围,等待其中的所有子协程完成;如果有一个子协程出现异常,coroutinescope 内其他子协程会被一起取消
supervisorscope类似于 coroutinescope,但子协程失败不会取消其他子协程
suspendcoroutine类似于 coroutinescope,但是可以手动控制挂起与恢复
async和await
自定义scope
自定义线程池
import kotlinx.coroutines.*
import java.util.concurrent.executors

fun main() {
	val customthreadpool = executors.newfixedthreadpool(4).ascoroutinedispatcher()

	runblocking {
		launch(customthreadpool) {
			println("custom thread pool: ${thread.currentthread().name}")
		}
	}

	// 关闭自定义线程池
	(customthreadpool.executor as? executors)?.shutdown()
}
(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com