Concurrency in Golang

 Concurrency in Golang

What is concurrency

Concurrency is the ability of a system or program to have multiple tasks in progress
at the same time. 
It is a way of designing and organizing a system to allow multiple tasks to overlap in their execution, rather than executing them sequentially one after the other.

Concurrency is often used in the context of computer programming, 
where it allows multiple tasks to be performed concurrently within a single program. 
It can be used to improve the performance and responsiveness of a program by allowing it to
perform multiple tasks at the same time, rather than executing them one at a time in
a sequential manner.

There are several different ways to implement concurrency in a program, including:

Multi threading: This involves dividing a program into multiple threads of
execution that can run concurrently on separate cores or processors.

Asynchronous programming: This involves using asynchronous functions or
methods that allow a program to perform multiple tasks concurrently, without
blocking the execution of the program.

Event-driven programming: This involves using a programming model in which
the program responds to events or triggers, allowing it to perform multiple tasks
concurrently in response to different events.

Concurrency can be useful in a variety of contexts, including improving the
performance and responsiveness of programs that perform multiple tasks, or that
need to interact with external systems or resources. However, it can also introduce
complexity and the need for careful design and testing in order to avoid race
conditions and other issues that can arise when multiple tasks are running concurrently.

What is difference between Concurrency and
Parallelism

Concurrency refers to the ability of a system to have multiple tasks in progress at the
same time. It is about dealing with multiple tasks concurrently, even if they are not
running simultaneously on separate processors or cores. Concurrency is a way of
designing and organizing a system to allow multiple tasks to overlap in their execution.

Parallelism, on the other hand, refers to the ability of a system to actually execute
multiple tasks simultaneously, using multiple processors or cores. Parallelism
involves physically running multiple tasks at the same time on different processors or
cores, in order to speed up the overall execution of the tasks.

To summarize, concurrency is about managing multiple tasks concurrently, while
parallelism is about executing multiple tasks simultaneously.

It is important to note that concurrency and parallelism are not the same thing, and
they can be used together or separately depending on the needs of a particular
system or application. 

In some cases, a system may use concurrency to manage multiple tasks concurrently, 
but only execute them sequentially on a single processor or core. 

In other cases, a system may use both concurrency and parallelism to manage and 
execute multiple tasks concurrently on multiple processors or cores.

How concurrency achieved in golang

In Go, concurrency is achieved through the use of goroutines and channels.

A goroutine is a lightweight concurrent function execution. Goroutines are created
using the go keyword, followed by a function call. When a goroutine is created, it is
scheduled to run concurrently with the calling function.

Channels are a way to communicate between goroutines. They are used to send
and receive values between goroutines. Channels can be created using the make
function, and values can be sent and received using the <- operator.
Here is an example of how to use goroutines and channels in Go:



In this example, the main function creates a channel of type int and launches a
goroutine that sends the value 42 to the channel.

The main function then receives the value from the channel and prints it.

Goroutines and channels are powerful tools for building concurrent programs in Go.
They allow developers to easily create and manage concurrent tasks, 
and communicate between them using channels.

Goroutines

Go routines are lightweight concurrent threads that can be used to perform tasks
concurrently in Go. Go routines are managed by the Go runtime, and can be created
and scheduled using the go keyword.

Here is an example of how to create and use Go routines in Go:

In this example, the printNumbers function is a Go routine that sends a series of
numbers to a channel c . 
The main function creates the channel and starts the Goroutine using the go keyword. 
The main function then iterates over the channel, printing the numbers as they are received.

Go routines are useful for writing concurrent programs that can take advantage of
multiple CPU cores or that need to perform multiple tasks simultaneously.
They are a key feature of Go's concurrency model, and are used in many types of
applications.
Overall, Goroutines are a powerful and flexible tool for writing concurrent programs
in Go.

To communicate between Go routines, you can use a variety of techniques,
including:
1. Channels: Channels are a built-in data structure in Go that allow Go routines to
send and receive values of a specific type. You can use channels to synchronize
Go routines, pass data between them, or signal the completion of a task.

2. Shared variables: You can use shared variables to communicate between Go
routines, but you need to use synchronization mechanisms such as mutexes to
protect against race conditions.

3. Wait groups: Wait groups are a built-in data structure in Go that allow you to
wait for a group of Go routines to finish. You can use wait groups to synchronize
Go routines, or to signal the completion of a task.

4. Timers and Tickers: Timers and tickers are built-in data structures in Go that
allow you to schedule tasks or send signals at regular intervals. You can use
timers and tickers to synchronize Go routines or to trigger actions based on time.
Overall, there are many ways to communicate between Go routines in Go, and the
appropriate method depends on the specific requirements of your application.

Interview questions

1.What is difference between Concurrency and Parallelism

2.What is concurrency

3.How concurrency achieved in golang


Comments

Popular posts from this blog

Golang Interview Questions Part 1 Theory Top 50 Question

Complete Golang Development setup on Linux (Ubuntu 20.04 )

Best GitHub repositories for Go