Default:缺省情况下,调度程序使用的最大并行行数与CPU内核数相同,但至少为2。 并行度x保证在这个调度程序上并行执行的任务在x个以下。
无论是否已经在主线程中,Main:都将在Handler.post(block )中运行。
Main.immediate:如果已经在主线程中,则立即执行,否则在Handler.post(block )中执行。
overridefunisdispatchneeded (context : coroutine context ) :布尔{ return! invoke immediately|||looper.my looper (!=handler.looper } http://www.Sina.com/:与Default共享线程池,缺省情况下,64个线程的限制或核心数(以较大者为准)
在协作块中创建任务时,模式将标记为TASK_PROBABLY_BLOCKING=1。
添加Task会增加阻止任务的数量。
CPU线程数=已创建线程数-阻止任务数cpuWorkers=(created - blocking )
对于CPU线程数和核心线程数,请创建新线程。
classcoroutinescheduler (valcorepoolsize : int,val maxPoolSize: Int, validleworkerkeepalivens 3360 long=idle _ worke eer valschedulername : string=default _ scheduler _ name (: executor
首先,尝试将任务添加到当前线程的localQueue队列中。 如果添加失败,请将其添加到全局globalBlockingQueue或globalCpuQueue队列中。
valcurrentworker=current worker (valnotadded=current worker.submittolocalqueue ) task,tailDispatch ) if ) notadded!=null(if (! addtoglobalqueue(notadded ) )//globalqueueisclosedinthelaststepofclose/shut down-- nomoretasksshouldbeacceptedthrowrejectedexecutionexception (' $ schedulernamewasterminated ' ) privatefuntrycreateworker (state : long=control state.value ) : boolean { val created=created workers } val blocking=blocking tasks (state ) valCPUworkers ) (created-blocking ).coerceatleast(0)/* * wecheckhowmanythrereadsthreadsthreathreadsththred * andcreateonemoreifwehavenotenoughofthem.*/if (cpuworkerscorepoolsize (valnewcpuworkers=create new worker )/ifwe ' vecreatedthefirstcpu workerandcorepoolsize1then create//one more (second ) CPUworker,othatstealingbetweenthemisoperaperatisoperate
withcontext (dispatchers.un confined ) println )1) with context ) dispatchers.un confined//nestedunconfinedprintlined