0
0
Goprogramming~10 mins

Why concurrency is needed in Go - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why concurrency is needed
Start Program
Task 1 Begins
Task 2 Begins
Tasks run sequentially?
Wait long
Slow program
Better resource use
Program ends
Shows how running tasks one after another slows a program, but running them at the same time (concurrency) makes it faster and uses resources better.
Execution Sample
Go
package main
import (
 "fmt"
 "time"
)
func main() {
 fmt.Println("Start")
 time.Sleep(2 * time.Second) // Task 1
 fmt.Println("Task 1 done")
 time.Sleep(2 * time.Second) // Task 2
 fmt.Println("Task 2 done")
}
This code runs two tasks one after another, each taking 2 seconds, so total time is about 4 seconds.
Execution Table
StepActionTime Passed (seconds)Output
1Print "Start"0Start
2Sleep 2 seconds (Task 1)2
3Print "Task 1 done"2Task 1 done
4Sleep 2 seconds (Task 2)4
5Print "Task 2 done"4Task 2 done
6Program ends4
💡 Both tasks run one after another, total time is 4 seconds.
Variable Tracker
VariableStartAfter Task 1After Task 2End
Time Passed (seconds)0244
Output"""Start\nTask 1 done\n""Start\nTask 1 done\nTask 2 done\n""Start\nTask 1 done\nTask 2 done\n"
Key Moments - 2 Insights
Why does the program take 4 seconds instead of 2?
Because Task 1 and Task 2 run one after another (see execution_table rows 2 and 4), so their times add up.
What if tasks could run at the same time?
Then total time would be about 2 seconds, not 4, because both tasks happen together, saving time.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed at step 3?
A"Task 2 done"
B"Task 1 done"
C"Start"
DNothing
💡 Hint
Check the Output column at step 3 in execution_table.
At which step does the program finish sleeping for Task 2?
AStep 4
BStep 2
CStep 5
DStep 6
💡 Hint
Look at the Action and Time Passed columns in execution_table.
If Task 1 and Task 2 ran concurrently, how would total time change?
AIt would stay 4 seconds
BIt would be about 1 second
CIt would be about 2 seconds
DIt would be 0 seconds
💡 Hint
Think about running tasks at the same time as explained in key_moments.
Concept Snapshot
Concurrency lets multiple tasks run at the same time.
Without concurrency, tasks run one after another, adding up time.
With concurrency, total time can be shorter.
This improves program speed and resource use.
In Go, concurrency is easy with goroutines.
Full Transcript
This example shows why concurrency is needed. The program runs two tasks, each taking 2 seconds, one after another. So total time is 4 seconds. If tasks ran at the same time, total time would be about 2 seconds. Concurrency helps programs run faster and use resources better by doing multiple things at once.