Package 

Interface ExecutionContextManager

  • All Implemented Interfaces:

    
    public interface ExecutionContextManager
    
                        

    Holds and exposes information about current execution context in a thread-local storage

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Constructor Detail

    • Method Detail

      • pushToContext

         abstract Unit pushToContext(String key, Object value)

        This method and popFromContext do the same thing as withContext, it's highly recommended to use them as below:

        pushToContext(myKey, myValue)
        try {
            // action
        } finally {
            popFromContext(myKey)
        }

        These methods are introduced in order to avoid new lambda object construction during frequent calls to withContext.

        Note: it's recommended to use withContext whenever possible as it guarantees that target data is popped out from active context once target action is done. This explicit push/pop operations are only for situations when we fine-tune application to avoid memory consumption as much as possible.

      • getCurrentCoroutineContextElements

         abstract CoroutineContext getCurrentCoroutineContextElements()

        Exposes all current context information as CoroutineContext.

        Currently execution context is based on thread and ThreadLocal but different coroutines can be executed on different threads.

        If we want to keep using currentContext data in coroutines, we should include currentCoroutineContextElements as part of coroutine context, for example, as below:

        runBlocking {
            launch(Dispatchers.IO + executionContextManager.currentCoroutineContextElements) {
                // coroutine logic
            }
        }