Highlights
- Introduction to Kotlin Coroutines
- Explore styles of concurrency
- Understand suspension mechanics
- Dive into coroutines under the hood
- Starting coroutines: builders & scope functions
- Structured concurrency & CoroutineContext
- Exception handling & constructing Coroutine Scope
- Testing Kotlin Coroutines
- Utilize Channels & Actors
- Select expressions & Flow fundamentals
- Flow lifecycle & processing
- StateFlow & SharedFlow
- Real-world use cases overview
Course Details
Module 1: Introduction to Concurrency in Kotlin
1.1 Styles of Concurrency
- Overview of Concurrency Models: Threads, Callbacks, Futures/Promises.
- Concurrency Challenges: Race conditions, deadlocks, thread management.
- Why Coroutines?: Benefits over traditional concurrency methods.
1.2 Overview of Common Use-Cases
- Asynchronous Programming: Network calls, file I/O operations.
- Parallel Processing: CPU-intensive tasks.
- UI Interactions: Updating UI elements without blocking the main thread.
Module 2: Understanding Coroutines
2.1 Understanding How Suspension Works
- Suspend Functions: Definition and usage.
- Continuation Passing: How suspension resumes.
- Call Stack and Coroutines: Differences from traditional functions.
2.2 Coroutines Under the Hood
- Coroutine Mechanics: How Kotlin implements coroutines.
- Lightweight Threads: Memory and performance benefits.
- Coroutine States: Created, Active, Completed, Cancelled.
2.3 Starting Coroutines: Builders and Scope Functions
- Coroutine Builders: launch, async, runBlocking.
- Scope Functions: coroutineScope, supervisorScope.
- Parent and Child Coroutines: Hierarchical structure.
2.4 Structured Concurrency
- Principles of Structured Concurrency: Keeping coroutines manageable.
- Scope Management: Ensuring coroutines complete or cancel together.
- Error Propagation: Handling exceptions in coroutine hierarchies.
Module 3: Coroutine Context and Dispatchers
3.1 CoroutineContext
- Context Elements: Job, Dispatcher, ExceptionHandler.
- Manipulating Context: Adding and combining context elements.
- Thread Local Data: Sharing data between coroutines.
3.2 Dispatchers
- Default Dispatchers: Dispatchers.Default, Dispatchers.IO, Dispatchers.Main.
- Custom Dispatchers: Creating and using custom thread pools.
- Dispatcher Selection: Choosing the right dispatcher for tasks.
3.3 Understanding Job
- Job Hierarchy: Parent and child jobs.
- Job Lifecycle: States and transitions.
- Job Control: Cancelling and joining jobs.
3.4 Cancellation
- Cooperative Cancellation: How coroutines handle cancellation.
- Cancellation Exceptions: Understanding CancellationException.
- Timeouts: Using withTimeout and withTimeoutOrNull.
3.5 Exception Handling
- Structured Exception Handling: try-catch within coroutines.
- Exception Propagation: How exceptions travel in coroutine scopes.
- Supervisor Jobs: Isolating failures with SupervisorJob and supervisorScope.
3.6 Constructing Coroutine Scope
- Global Scope vs. Custom Scopes: Best practices.
- Lifecycle-Aware Scopes: Tying coroutine scopes to application components.
- Scope Cancellation: Managing scope lifecycle and cleanup.
Module 4: Advanced Coroutine Concepts
4.1 Testing Kotlin Coroutines
- Testing Strategies: Unit testing suspend functions and coroutines.
- Test Dispatchers: Controlling coroutine execution in tests.
- Mocking and Verification: Using libraries like MockK or Mockito.
4.2 Channels
- Introduction to Channels: Communication between coroutines.
- Types of Channels: Rendezvous, buffered, conflated, unlimited.
- Channel Operations: Sending, receiving, closing channels.
4.3 Actors
- Actor Model: Encapsulating state and behavior.
- Implementing Actors: Using actor coroutine builder.
- Use-Cases for Actors: Managing shared mutable state safely.
4.4 Select Expression
- Selective Communication: Handling multiple channel operations.
- Timeouts and Defaults: Providing fallbacks in select expressions.
- Use-Cases: Multiplexing input from multiple channels.
Module 5: Working with Flows
5.1 Understanding Flow
- Cold Streams: Difference between sequences and flows.
- Flow Builders: flow, flowOf, asFlow.
- Flow Collection: Terminal operators like collect, toList.
5.2 Flow Lifecycle
- Lifecycle of a Flow: Emission, collection, cancellation.
- Backpressure Handling: Strategies for controlling data flow.
- Exception Transparency: Handling errors in flows.
5.3 Flow Processing
- Intermediate Operators: map, filter, transform, flatMapConcat.
- Combining Flows: zip, combine, merging multiple flows.
- Flow Context: Controlling the execution context with flowOn.
5.4 StateFlow and SharedFlow
- StateFlow: Managing state in reactive applications.
- SharedFlow: Hot streams and event sharing.
- Comparison with Channels: When to use StateFlow or SharedFlow over channels.
Module 6: Best Practices
6.1 Concurrency Best Practices
- Avoiding Common Pitfalls: Blocking threads, unstructured coroutines.
- Optimizing Performance: Efficient use of dispatchers and contexts.
- Thread Safety: Managing shared resources.
6.2 Coroutines in Real-world Applications
- Integration with Android: Lifecycle-aware components.
- Server-side Applications: Using coroutines with Ktor.
- Migration Strategies: Converting callback-based code to coroutines.
6.3 Code Maintainability
- Clean Code Principles: Writing readable and maintainable coroutine code.
- Documentation: Commenting and documenting asynchronous flows.
- Code Reviews: What to look for in coroutine implementations.
Who should attend
Feedback
4.8 out of 5 average
"Our tailored course provided a well rounded introduction and also covered some intermediate level topics that we needed to know. Clive gave us some best practice ideas and tips to take away. Fast paced but the instructor never lost any of the delegates"
Brian Leek, Data Analyst, May 2022
“JBI did a great job of customizing their syllabus to suit our business needs and also bringing our team up to speed on the current best practices. Our teams varied widely in terms of experience and the Instructor handled this particularly well - very impressive”
Brian F, Team Lead, RBS, Data Analysis Course, 20 April 2022