-
public final class ThreadUtilsKt
-
-
Method Summary
Modifier and Type Method Description final static UnitsuspendifyBlocking(SuspendFunction0<Unit> block)Allows a non-suspending function to create a scope that can call suspending functions. final static UnitsuspendifyOnMain(SuspendFunction0<Unit> block)Allows a non suspending function to create a scope that can call suspending functions while on the main thread. final static UnitsuspendifyOnThread(Integer priority, SuspendFunction0<Unit> block)Allows a non suspending function to create a scope that can call suspending functions. final static UnitsuspendifyOnThread(Integer priority, SuspendFunction0<Unit> block, Function0<Unit> onComplete)Allows a non suspending function to create a scope that can call suspending functions. final static UnitsuspendifyOnThread(String name, Integer priority, SuspendFunction0<Unit> block)Allows a non suspending function to create a scope that can call suspending functions. -
-
Method Detail
-
suspendifyBlocking
final static Unit suspendifyBlocking(SuspendFunction0<Unit> block)
Allows a non-suspending function to create a scope that can call suspending functions. This is a blocking call, which means it will not return until the suspending scope has been completed. The current thread will also be blocked until the suspending scope has completed.
Note: This can be very dangerous!! Blocking a thread (especially the main thread) has the potential for a deadlock. Consider this code that is running on the main thread:
suspendifyOnThread { withContext(Dispatchers.Main) { } }The
withContextwill suspend until the main thread is available, but the main thread is parked via thissuspendifyBlocking. This will never recover.
-
suspendifyOnMain
final static Unit suspendifyOnMain(SuspendFunction0<Unit> block)
Allows a non suspending function to create a scope that can call suspending functions while on the main thread. This is a nonblocking call, the scope will start on a background thread and block as it switches over to the main thread context. This will return immediately!!!
-
suspendifyOnThread
final static Unit suspendifyOnThread(Integer priority, SuspendFunction0<Unit> block)
Allows a non suspending function to create a scope that can call suspending functions. This is a nonblocking call, which means the scope will run on a background thread. This will return immediately!!!
-
suspendifyOnThread
final static Unit suspendifyOnThread(Integer priority, SuspendFunction0<Unit> block, Function0<Unit> onComplete)
Allows a non suspending function to create a scope that can call suspending functions. This is a nonblocking call, which means the scope will run on a background thread. This will return immediately!!! Also provides an optional onComplete.
- Parameters:
priority- The priority of the background thread.block- A suspending lambda to be executed on the background thread.onComplete- An optional lambda that will be invoked on the same background thread after block has finished executing.
-
suspendifyOnThread
final static Unit suspendifyOnThread(String name, Integer priority, SuspendFunction0<Unit> block)
Allows a non suspending function to create a scope that can call suspending functions. This is a nonblocking call, which means the scope will run on a background thread. This will return immediately!!!
-
-
-
-