Concept Flow - Task for launching concurrent work
Start
Create Task
Task Runs Concurrently
Task Completes
Main Code Continues
This flow shows how a Task is created to run code concurrently, allowing the main program to continue without waiting.
Task {
print("Hello from Task")
}
print("Hello from Main")| Step | Action | Code Executed | Output | Notes |
|---|---|---|---|---|
| 1 | Create Task | Task { print("Hello from Task") } | Task is created but runs concurrently | |
| 2 | Main code runs | print("Hello from Main") | Hello from Main | Main thread prints immediately |
| 3 | Task runs | print("Hello from Task") | Hello from Task | Task prints concurrently, may appear before or after main output |
| 4 | End | Program ends after both prints |
| Variable | Start | After Task Created | After Main Print | After Task Print | Final |
|---|---|---|---|---|---|
| Task | nil | Task instance created | Task running concurrently | Task completed | nil |
Task { ... } launches concurrent work in Swift.
The main code continues immediately without waiting.
Task runs asynchronously and can print or do work.
Outputs may appear in any order.
Use Task to run code concurrently simply.