// ĠCopyright Ġ 2 0 0 0 - 2 0 1 9 ĠJetBrains Ġs . r . o . ĠUse Ġof Ġthis Ġsource Ġcode Ġis Ġgoverned Ġby Ġthe ĠApache Ġ 2 . 0 Ġlicense Ġthat Ġcan Ġbe Ġfound Ġin Ġthe ĠLICENSE Ġfile . Ċ package Ġcom . intellij . application Ċ Ċ import Ġcom . intellij . openapi . application . ex . Application Manager Ex Ċ import Ġcom . intellij . openapi . diagnostic . Logger Ċ import Ġcom . intellij . util . concurrency . App Executor Util Ċ import Ġkotlinx . coroutines . Coroutine Dispatcher Ċ import Ġkotlinx . coroutines . Coroutine ExceptionHandler Ċ import Ġkotlinx . coroutines . Runnable Ċ import Ġkotlin . coroutines . Coroutine Context Ċ Ċ @ Jvm Over loads Ċ inline Ġfun Ġrun In Allow Save Mode ( is Save Allowed : ĠBoolean Ġ= Ġtrue , Ġtask : Ġ() Ġ-> ĠUnit ) Ġ{ ĊĠ Ġval Ġapp Ġ= ĠApplication Manager Ex . getApplication Ex () ĊĠ Ġif Ġ( is Save Allowed Ġ== Ġapp . is Save Allowed ) Ġ{ ĊĠĠĠ Ġtask () ĊĠĠĠ Ġreturn ĊĠ Ġ} ĊĊĠ Ġapp . is Save Allowed Ġ= Ġis Save Allowed ĊĠ Ġtry Ġ{ ĊĠĠĠ Ġtask () ĊĠ Ġ} ĊĠ Ġfinally Ġ{ ĊĠĠĠ Ġapp . is Save Allowed Ġ= Ġ! is Save Allowed ĊĠ Ġ} Ċ } Ċ Ċ /** Ċ Ġ* ĠExecute Ġcoroutine Ġon Ġp ooled Ġthread . ĠUn caught Ġerror Ġwill Ġbe Ġlogged . Ċ Ġ* Ċ Ġ* Ġ@ see Ġcom . intellij . openapi . application . Application . execute On P ooled Thread Ċ Ġ*/ Ċ val Ġp ooled Thread Context : ĠCor outine Context Ġ= ĠApplication ThreadPool Dispatcher () Ġ+ ĠCor outine ExceptionHandler Ġ{ Ġ_, Ġthrowable Ġ-> ĊĠ ĠLogger . getInstance ("# com . intellij . application . impl . Application Impl "). error ( throwable ) Ċ } Ċ Ċ // Ġno Ġneed Ġto Ġimplement Ġis Dispatch Needed Ġ- ĠKotlin Ġcorrectly Ġuses Ġthe Ġsame Ġthread Ġif Ġcor outines Ġexecutes Ġse quent ially , Ċ // Ġand Ġif Ġlaunch / async Ġis Ġused , Ġit Ġis Ġcorrect Ġand Ġexpected Ġthat Ġcoroutine Ġwill Ġbe Ġdis patched Ġto Ġanother Ġp ooled Ġthread . Ċ private Ġclass ĠApplication ThreadPool Dispatcher Ġ: ĠCor outine Dispatcher () Ġ{ ĊĠ Ġoverride Ġfun Ġdispatch ( context : ĠCor outine Context , Ġblock : ĠRunnable ) Ġ{ ĊĠĠĠ ĠApp Executor Util . getApp ExecutorService (). execute ( block ) ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun ĠtoString () Ġ= ĠApp Executor Util . getApp ExecutorService (). toString () Ċ }