ProgramGuru

Concurrency vs Parallelism

Start with Concurrency vs Parallelism

In this lesson, explore concurrency vs parallelism through one small Go program, and connect its output to the language rules behind it.

What you’ll learn

  • Explain the main purpose of concurrency vs parallelism.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

Concurrency concerns how independent work is structured; parallelism means operations actually execute at the same time on different processors. Go's scheduler decides when goroutines run, and available CPUs influence parallel execution.

Never infer speedup from the number of goroutines. Coordination overhead and shared resources can dominate small tasks.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for parallelismConcurrency can exist without simultaneous parallel execution.Independent tasksStep 1Scheduler interleavesStep 2CPUs may overlapStep 3Wait for completionStep 4Concurrency can exist without simultaneous parallel execution.
Concurrency can exist without simultaneous parallel execution.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import (
	"fmt"
	"runtime"
	"sync"
)

func main() {
	var wg sync.WaitGroup
	values := make([]int, 2)
	for i := range values {
		wg.Add(1)
		go func(index int) { defer wg.Done(); values[index] = index + 10 }(i)
	}
	wg.Wait()
	fmt.Println("Results:", values)
	fmt.Println("CPU count known:", runtime.NumCPU() > 0)
}

Output

Results: [10 11]
CPU count known: true

Trace what the program does

Distinct indexes are written without overlap. Wait blocks until both tasks finish. The program reports whether a CPU count is available, not a fixed machine-specific count.

Avoid common mistakes

  • Concurrency does not promise two operations execute simultaneously.
  • Avoid comparing runtimes without realistic work and measurements.

Try it yourself

Try the program with four goroutines, each writing a different slot; predict the result.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

What distinguishes parallelism from concurrency?
Choose one answer. You can try again.

Answer and explanation

Simultaneous execution on hardware

Correct. Distinct indexes are written without overlap.

What you should remember

You have practiced Concurrency vs Parallelism in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Introduction to Goroutines →