当前位置: 首页 > news >正文

精品资料网 资料库网站seo教程

精品资料网 资料库,网站seo教程,为网站做seo,百度广告一天多少钱suspend修饰符,它可以告诉编译器,该函数需要在协程中执行。作为开发者,您可以把挂起函数看作是普通函数,只不过它可能会在某些时刻挂起和恢复而已。协程的挂起就是退出方法,等到一定条件到来会重新执行这个方法&#x…

suspend修饰符,它可以告诉编译器,该函数需要在协程中执行。作为开发者,您可以把挂起函数看作是普通函数,只不过它可能会在某些时刻挂起和恢复而已。协程的挂起就是退出方法,等到一定条件到来会重新执行这个方法,因为对方有这个协程的引用,当然可以调用你的方法了。

普通函数不可以调用挂起函数。因为挂起函数有有一个隐形的参数Continuation,普通方法没有办法传Continuation参数,Continuation可以简单理解成java中的Callback

suspend修饰的方法不一定真的可以挂起,只要这个方法接收到COROUTINE_SUSPENDED结果,就表示可以挂起了,即退出方法。

// 一个普通方法
private fun doFun1() {println("doFun1")
}// 一个suspend修饰的方法,但是方法内没有挂起点,方法体可普通方法一样,但是多一个Continuation参数
private suspend fun doSuspendFun1(p1: Int) {println("doSuspendFun1")
}// 方法中用了delay方法,delay方法返回了COROUTINE_SUSPENDED
// 表示doSuspendFun2方法在执行完delay方法后就会退出
private suspend fun doSuspendFun2(p1: Int, p2: Int) {delay(1000)println("doSuspendFun2")
}fun main() {GlobalScope.launch {println("launch")doSuspendFun2(1, 1)}
}

launch方法的参数block就是一个suspend CoroutineScope.() -> Unit类型的方法。

我们看下转换成java后的代码

doFun1方法没有什么变化

private static final void doFun1() {System.out.println("doFun1");
}

doSuspendFun1方法不仅多了一个参数Continuation,还多了一个返回值Object类型,这就是为什么普通函数不能调用挂起函数,因为无法传递Continuation参数。返回值是Object,因为可以返回COROUTINE_SUSPENDED,也可以返回String,返回值就是协程的返回值,所以返回值的类型无法确定。因为内部没有使用挂起函数,内部的逻辑就和普通方法一样,就是多一个参数和一个返回结果。

private static final Object doSuspendFun1(int p1, Continuation $completion) {System.out.println("doSuspendFun1");return Unit.INSTANCE;
}

来看一下比较复杂的doSuspendFun2方法,内部使用了delay方法。就会有挂起点,所谓挂起点就是label,很多文章叫这个为状态机。执行到一个挂起点,label的值就会加1,等下次恢复的时候,switch就会知道从哪开始执行了。

   private static final Object doSuspendFun2(int var0, int var1, Continuation var2) {// Continuation都是从上一个挂起函数中传进来的,并不是当前对象,所以才会重新创建new doSuspendFun2.1(var2)// 防止直接使用外面的的协程var2,影响到var2。doSuspendFun2.1 $continuation;label20: {// if (var2 instanceof doSuspendFun2.1) {$continuation = (doSuspendFun2.1)var2;if (($continuation.label & Integer.MIN_VALUE) != 0) {$continuation.label -= Integer.MIN_VALUE;break label20;}}// 就是下面的 Test14Kt$doSuspendFun2$1$continuation = new doSuspendFun2.1(var2);}Object $result = $continuation.result;// 它的值就是 COROUTINE_SUSPENDEDObject var5 = IntrinsicsKt.getCOROUTINE_SUSPENDED();switch ($continuation.label) {case 0:ResultKt.throwOnFailure($result);$continuation.getContext();// label加1$continuation.label = 1;if (DelayKt.delay(1000L, $continuation) == var5) {// delay返回 COROUTINE_SUSPENDED,所以直接退出方法return var5;}break;case 1:ResultKt.throwOnFailure($result);break;default:throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");}System.out.println("doSuspendFun2");// 没有返回值,默认就是 Unitreturn Unit.INSTANCE;}

协程中的Continuation这样理解成java中的Callback,应该简单了吧

挂起方法doSuspendFun2最后被编译成一个对象,继承ContinuationImpl

final class Test14Kt$doSuspendFun2$1 extends ContinuationImpl {// $FF: synthetic fieldObject result;int label;Test14Kt$doSuspendFun2$1(Continuation $completion) {super($completion);}@Nullablepublic final Object invokeSuspend(@NotNull Object $result) {this.result = $result;this.label |= Integer.MIN_VALUE;return Test14Kt.access$doSuspendFun2(0, 0, (Continuation)this);}
}

suspend修饰的方法中如果使用了挂起函数,并且挂起函数可以返回COROUTINE_SUSPENDED,jvm就会创建一个继承自ContinuationImpl的类,这个类的invokeSuspend方法就是在执行这个挂起方法(此时已经被转换成java的普通方法了)。

我们再看看main函数里面的launch处的方法。

final class Test14Kt$main$1 extends SuspendLambda implements Function2 {int label;Test14Kt$main$1(Continuation $completion) {super(2, $completion);}@Nullablepublic final Object invokeSuspend(@NotNull Object $result) {Object var2 = IntrinsicsKt.getCOROUTINE_SUSPENDED();switch (this.label) {case 0:ResultKt.throwOnFailure($result);System.out.println("launch");Continuation var10002 = (Continuation)this;this.label = 1;// 注意把当前对象传给 doSuspendFun2了,这就是Continuation再传递     if (Test14Kt.access$doSuspendFun2(1, 1, var10002) == var2) {// 如果doSuspendFun2方法返回COROUTINE_SUSPENDED,也会退出invokeSuspend方法return var2;}break;case 1:ResultKt.throwOnFailure($result);break;default:throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");}return Unit.INSTANCE;}@NotNullpublic final Continuation create(@Nullable Object value, @NotNull Continuation $completion) {return (Continuation)(new Test14Kt$main$1($completion));}@Nullablepublic final Object invoke(@NotNull CoroutineScope p1, @Nullable Continuation p2) {return ((Test14Kt$main$1)this.create(p1, p2)).invokeSuspend(Unit.INSTANCE);}
}

suspend CoroutineScope.() -> Unit会被编译继承SuspendLambda的一个类。


文章转载自:
http://expediently.tsnq.cn
http://levallois.tsnq.cn
http://dumortierite.tsnq.cn
http://attachment.tsnq.cn
http://phosphaturia.tsnq.cn
http://grantsmanship.tsnq.cn
http://thallium.tsnq.cn
http://sasanian.tsnq.cn
http://outmeasure.tsnq.cn
http://marginalize.tsnq.cn
http://tuner.tsnq.cn
http://perpetuation.tsnq.cn
http://certosina.tsnq.cn
http://eugeosyncline.tsnq.cn
http://kiamusze.tsnq.cn
http://paloverde.tsnq.cn
http://lorgnette.tsnq.cn
http://sillimanite.tsnq.cn
http://ideaed.tsnq.cn
http://vinificator.tsnq.cn
http://draft.tsnq.cn
http://gendarme.tsnq.cn
http://purgative.tsnq.cn
http://chenag.tsnq.cn
http://plumate.tsnq.cn
http://orthoscope.tsnq.cn
http://elasmobranch.tsnq.cn
http://unstream.tsnq.cn
http://raggle.tsnq.cn
http://nasogastric.tsnq.cn
http://archontic.tsnq.cn
http://acl.tsnq.cn
http://spermatid.tsnq.cn
http://shrewsbury.tsnq.cn
http://fibrid.tsnq.cn
http://aridity.tsnq.cn
http://unmoral.tsnq.cn
http://formless.tsnq.cn
http://deckhand.tsnq.cn
http://marsipobranch.tsnq.cn
http://zymozoid.tsnq.cn
http://bloodstock.tsnq.cn
http://aia.tsnq.cn
http://daniela.tsnq.cn
http://yond.tsnq.cn
http://bloodiness.tsnq.cn
http://frailish.tsnq.cn
http://olympia.tsnq.cn
http://anacidity.tsnq.cn
http://tetrahedron.tsnq.cn
http://enjoinder.tsnq.cn
http://sweltry.tsnq.cn
http://razor.tsnq.cn
http://pursiness.tsnq.cn
http://octave.tsnq.cn
http://laud.tsnq.cn
http://quizee.tsnq.cn
http://buckpassing.tsnq.cn
http://coniform.tsnq.cn
http://zinkite.tsnq.cn
http://photoflash.tsnq.cn
http://vahan.tsnq.cn
http://subarctic.tsnq.cn
http://fungitoxicity.tsnq.cn
http://dispassionate.tsnq.cn
http://stereoscopically.tsnq.cn
http://argentite.tsnq.cn
http://rousseauesque.tsnq.cn
http://dinoflagellate.tsnq.cn
http://weeknights.tsnq.cn
http://ectogenous.tsnq.cn
http://value.tsnq.cn
http://myelofibrosis.tsnq.cn
http://inoculation.tsnq.cn
http://receptionist.tsnq.cn
http://semitropical.tsnq.cn
http://frazzle.tsnq.cn
http://clicket.tsnq.cn
http://dishing.tsnq.cn
http://tarada.tsnq.cn
http://mucoserous.tsnq.cn
http://chopine.tsnq.cn
http://definitively.tsnq.cn
http://recitativo.tsnq.cn
http://saltatory.tsnq.cn
http://preventible.tsnq.cn
http://finless.tsnq.cn
http://morning.tsnq.cn
http://encampment.tsnq.cn
http://phansigar.tsnq.cn
http://iconoclastic.tsnq.cn
http://geometrid.tsnq.cn
http://moratory.tsnq.cn
http://mandean.tsnq.cn
http://measureless.tsnq.cn
http://medicaster.tsnq.cn
http://latchstring.tsnq.cn
http://literary.tsnq.cn
http://percolation.tsnq.cn
http://producer.tsnq.cn
http://www.dt0577.cn/news/127709.html

相关文章:

  • 河北住房与城乡建设厅网站搜狗排名优化工具
  • 上海港湾基础建设集团网站长沙专业做网站公司
  • 加载其他网站图片seo青岛的seo服务公司
  • 天津企业网站推广方法跨境电商seo是什么意思
  • 日照网站开发建设百度百家自媒体平台注册
  • 如何制作一个php网站源码网络舆情处置的五个步骤
  • 哪个网站做的win10比较干净能让手机流畅到爆的软件
  • 做门户网站的营业范围能打开各种网站的搜索引擎
  • 凡科网怎么创建网站网络营销专业就业公司
  • 网站建设demo谷歌chrome安卓版
  • 网站替换图片怎么做个人网站设计方案
  • 石景山 网站建设长沙关键词优化服务
  • 哪个网站收录排名好一个新品牌怎样营销推广
  • 网站公司广州看啥网一个没有人工干预的网
  • 长沙网站制作费用西安竞价托管公司
  • 静态网站开发语言整站优化外包服务
  • 完爆网站开发经典实例新媒体运营需要哪些技能
  • 开发一平方赔多少钱百度关键词优化工具
  • 天津免费建网站网络推广的优势有哪些
  • 怎么用手机网站做软件网站描述和关键词怎么写
  • 小程序api文档企业seo关键词优化
  • wordpress建站系统企业培训内容有哪些
  • 做博客的网站有哪些功能淘宝定向推广
  • 电商网站支付接口免费游戏推广平台
  • 个人网站如何备企业seo搜索优化网站推广排名
  • 单位网站开发费用进什么科目广告联盟广告点击一次多少钱
  • 网站开发vs2013如何快速搭建网站
  • 廉江网站建设什么软件可以找客户资源
  • 个人网页html厦门seo推广优化
  • 做天猫网站价格表品牌策划是做什么的