首页
About
友情链接
Search
1
V2ray配置文件翻译
1,373 阅读
2
draw.io(diagrams.net) - 免费开源全平台绘图软件
1,109 阅读
3
Android 11 中的软件包可见性
922 阅读
4
Windows常用软件
838 阅读
5
Android Studio单元测试的编码过程
662 阅读
Uncategorized
Software
Android
Linux
Communist Party of China
Docker
Windows
Tools
登录
Search
Leon
累计撰写
35
篇文章
累计收到
23
条评论
首页
栏目
Uncategorized
Software
Android
Linux
Communist Party of China
Docker
Windows
Tools
页面
About
友情链接
搜索到
35
篇与
Leon
的结果
2023-04-08
此内容被密码保护
加密文章,请前往内页查看详情
2023年04月08日
7 阅读
0 评论
0 点赞
2023-03-03
Typora内测版
https://wwwv.lanzouw.com/b0bg3wfcf密码:hrgc
2023年03月03日
115 阅读
0 评论
0 点赞
2023-03-02
Windows 10 右键菜单 新建项添加md文件(Typora)
手动修改注册表首先请确保已安装Typorawin + R输入【regedit】定位路径 【计算机HKEY_CLASSES_ROOT.md】点击.md文件夹, 双击右侧(默认)项,将数值数据改为【Typora.md】右键.md文件夹 => 新建 => 项,把新建的项命名为【ShellNew】右键ShellNew => 新建 => 字符串值,将该字符串值名称改为【NullFile】关闭注册表即可一键插入新建文本文档,输入以下内容,命名后缀名为.reg,右键合并。Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\.md\ShellNew] "NullFile"="" "FileName"="template.md"
2023年03月02日
227 阅读
0 评论
0 点赞
2023-01-28
于广友大傻逼
暂无简介
2023年01月28日
145 阅读
0 评论
2 点赞
2022-11-04
Kotlin协程
Kotlin协程就像 Java 中的 Executor 和 Android 中的 AsyncTask,Kotlin 中的协程也有对 Thread API 的封装,让我们可以在写代码时,不用关注多线程就能够很方便地写出并发操作。Kotlin协程是由官方提供的一套线程API,使用看起来同步方式写异步代码——非堵塞式挂起。消除回调launch({ val user = api.getUser() // 网络请求(IO 线程) nameTv.text = user.name // 更新 UI(主线程) }) api.getUser() .enqueue(object : Callback<User> { override fun onResponse(call : Call<User>, response:Response<User>){ runOnUiThread { nameTv.text = response.body()?.name } } })suspend 是 Kotlin 协程最核心的关键字,代码执行到 suspend 函数的时候会挂起,并且这个挂起是非阻塞式的,它不会阻塞你当前的线程。launch ,async 或者其他函数创建的协程,在执行到某一个 suspend 函数的时候,这个协程会被suspend,也就是被挂起。GlobalScope.launch(Dispatchers.Main) { val image = suspendingGetImage(imageId) // 获取图片 avatarIv.setImageBitmap(image) // 显示出来 } suspend fun suspendingGetImage(id: String) = withContext(Dispatchers.IO) { ... } suspend fun suspendUntilDone() { while (!done) { delay(5) } }但是suspend fun suspendingPrint() { println("Thread: ${Thread.currentThread().name}") }输出的结果还是在主线程。通过 withContext 源码可以知道,它本身就是一个挂起函数,它接收一个 Dispatcher 参数,依赖这个 Dispatcher 参数的指示,协程被挂起,然后切到别的线程。public suspend fun <T> withContext( context: CoroutineContext, block: suspend CoroutineScope.() -> T ): T { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return suspendCoroutineUninterceptedOrReturn sc@ { uCont -> // 计算新上下文 val oldContext = uCont.context val newContext = oldContext + context // 始终检查新上下文的取消 newContext.ensureActive() // FAST PATH #1 -- 新上下文与旧上下文相同 if (newContext === oldContext) { val coroutine = ScopeCoroutine(newContext, uCont) return@sc coroutine.startUndispatchedOrReturn(coroutine, block) } // FAST PATH #2 -- 新dispatcher与旧dispatcher相同(发生了变化) // `equals` 被设计使用 if (newContext[ContinuationInterceptor] == oldContext[ContinuationInterceptor]) { val coroutine = UndispatchedCoroutine(newContext, uCont) // There are changes in the context, so this thread needs to be updated withCoroutineContext(newContext, null) { return@sc coroutine.startUndispatchedOrReturn(coroutine, block) } } // SLOW PATH -- 使用新的dispatcher val coroutine = DispatchedCoroutine(newContext, uCont) block.startCoroutineCancellable(coroutine, coroutine) coroutine.getResult() } }所以这个 suspend,其实并不是起到把任何协程挂起,或者说切换线程的作用。真正挂起协程这件事,是 Kotlin 的协程框架帮我们做的。所以我们想要自己写一个挂起函数,仅仅只加上 suspend 关键字是不行的,还需要函数内部直接或间接地调用到 Kotlin 协程框架自带的 suspend 函数才行。在 Kotlin 里,协程就是基于线程来实现的一种更上层的工具 API,类似于 Java 自带的 Executor 系列 API 或者 Android 的 Handler 系列 API。协程它不仅提供了方便的 API,在设计思想上是一个基于线程的上层框架协程就是切线程。挂起就是可以自动切回来的切线程。挂起的非阻塞式指的是它能用看起来阻塞的代码写出非阻塞的操作。
2022年11月04日
16 阅读
0 评论
0 点赞
1
2
3
...
7