Welcome to this post with a somewhat degrading title.
But, in this post I want to explain to you what these 2 characteristics of programming are in a very simple way, this time using my favorite programming language GOLANG.
Let's imagine a kitchen:
Cook a dish: This represents a task.
A cook: He is a processor.
Concurrence:
Several cooks in the kitchen: Each one preparing a different dish.
In Go: Every cook would be a goroutine. Although the kitchen (processor) only has one oven, cooks can work on their dishes simultaneously, spending time on other tasks while waiting for the oven to become available.
Parallelism:
Various ovens: Each cook has his own oven.
In Go: If we have multiple physical processors, each goroutine could run on a different processor, cooking several dishes at the same time in real life.
What's the difference?
Concurrency: Tasks are executed intertwined, giving the illusion of parallelism, even on a single processor.
Parallelism: Tasks run simultaneously on multiple processors, which significantly speeds up the process.
How to use them in Go?
Goroutines: They are like light threads. To create a goroutine, we simply use the go keyword before a function:
Let's see an example of how we can use goroutines in golang:
go func() { // Código que se ejecutará en una goroutine }()
Channels: These are pipes through which goroutines can communicate and synchronize.
Imagine that they are tubes to pass ingredients between the cooks
ch := make(chan int) go func() { chPractical example:
package main import ( "fmt" "time" ) func worker(id int, c chan int) { for n := range c { fmt.Printf("Worker %d received %d\n", id, n) time.Sleep(time.Second) } } func main() { c := make(chan int) for i := 1; iThe output of this code would be
Worker 1 received 1 Worker 2 received 2 Worker 3 received 3 Worker 4 received 4 Worker 5 received 5 Worker 1 received 6 Worker 2 received 7 Worker 3 received 8 Worker 4 received 9 Worker 5 received 10although sometimes it might look like this
Worker 5 received 1 Worker 1 received 3 Worker 2 received 2 Worker 4 received 5 Worker 3 received 4 Worker 3 received 6 Worker 5 received 10 Worker 2 received 8 Worker 4 received 7 Worker 1 received 9or like this
Worker 5 received 1 Worker 1 received 2 Worker 2 received 3 Worker 3 received 4 Worker 4 received 5 Worker 1 received 6 Worker 2 received 7 Worker 3 received 8 Worker 5 received 9 Worker 4 received 10Why does the output change every time I run the program?
The main reason why program output changes with each execution is due to the non-deterministic nature of concurrency.
Here's a breakdown of what's happening:
Create a channel: make(chan int) creates a channel of integers. This channel will be used for communication between goroutines.
Start goroutines: The loop for i := 1; i The worker function receives the ID and channel.
Send values to channel: The loop for n := 1; n 1 to 10 to channel.
Close the channel: The close(c) call closes the channel, indicating that no more values will be sent.
Receive values from channel: Each goroutine receives values from the channel using the for n := range c loop. When a value is received, it is printed to the console.
Wait for goroutines to finish: The time.Sleep(time.Second) call ensures that the main goroutine waits for the other goroutines to finish before exiting.
Until now:
We create 5 goroutines (cooks) that receive numbers through a channel.
We send numbers to the channel for the cooks to process.
The cooks work concurrently, processing the numbers as they receive them.Why use concurrency and parallelism in Go?
Better performance: Especially in I/O-bound tasks (such as reading files or making HTTP requests).
Increased responsiveness: The application can continue to respond to other requests while a task is blocked.
More scalable architectures: You can distribute work across multiple cores or machines.Remember!
Concurrency and parallelism are powerful tools, but they can also make code more complex to understand and debug. It is important to use them carefully and understand their implications.
Do you want to go deeper into a specific topic?
We can explore concepts like:
Synchronization: Mutexes, work groups, etc.
Concurrency patterns: Producer-consumer, pipeline, etc.
Concurrent Testing: How to Test Concurrent Code Effectively.Greetings,
Lucatonny RaudalesX/Twitter
Github
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3