Giter VIP home page Giter VIP logo

go-concurrency-parallelism's Introduction

Golang Concurrency vs Parallelism

Single Threaded vs Multi-Threaded Programs

Imagine You have to mine some ores using gophers, you'll have Gary as our worker:

gopher

A common way of performing this task on single Threaded applications is by using Gary through all the stages of the mining, like this: gary working

That's fine and all, but it really doesn't take advantage of maybe assigining different tasks to different workers, like this:

Gary Jane Peter

That's what we call concurrent programming, defined by:

In computer science, concurrency is the ability of different parts or units of a program, algorithm, or problem to be executed out-of-order or at the same time simultaneously partial order, without affecting the final outcome.

While parallelism is defined by:

The term Parallelism refers to techniques to make programs faster by performing several computations at the same time. This requires hardware with multiple processing units. In many cases the sub-computations are of the same structure, but this is not necessary.

Case Study : URL Status Checker


  1. Application Basic Logic:
flowchart LR
    A[URL Status Checker] ---> B[http request]
    B -.-> C[http://x-team.com]
    B -.-> D[http://github.com]
    B -.-> E[http://stackoverflow.com]
    B -.-> F[http://google.com]
    subgraph URLS
    C
    D
    E
    F
    end
  1. This program has a natural blocking architecture:
flowchart LR
C[http://x-team.com]
D[http://github.com]
E[http://stackoverflow.com]
F[http://google.com]
subgraph URLS
C
D
E
F
end
C --> |wait|D
D --> |wait|E
E --> |wait|F

URLS --> A[need to receive the http response before moving to the next URL]
  1. Why not try to optimize this?
flowchart 
C[http://x-team.com]
D[http://github.com]
E[http://stackoverflow.com]
F[http://google.com]

C -->|make request| X[return status]
D -->|make request| Y[return status]
E -->|make request| Z[return status]
F -->|make request| W[return status]

subgraph URLS
C
D
E
F
end

  1. Behind the Scenes
flowchart TB
subgraph Routines
A
B
C
D
E
W
Z
J
X
Y
end
A[CPU Core]<-->B[GO Scheduler]
B <-.-> C[Go Routine]
B <-.-> |pause/unpause|D[Go Routine]
B <-.-> E[Go Routine]
X[CPU Core]<-->Y[GO Scheduler]
Y <-.-> W[Go Routine]
Y <-.-> Z[Go Routine]
Y <-.-> J[Go Routine]
subgraph Running Program
A1[Main Routine]-->C1(Created when program is launched)
B1[Child Go Routine]-->D1
B2[Child Go Routine]-->D1
B3[Child Go Routine]-->D1
D1(Created by the go keyword)
end

  1. Why the child routines didn't run?
gantt
    title Time -->
    dateFormat DD-HH
    axisFormat %L
    section Program
    main routine:active,crit,mr,01-00,5h
    program exit: crit,after mr,4h
    child go routine:active,mr2,01-02,6h
    child go routine:active,mr2,01-02,7h
    child go routine:active,mr2,01-02,5h
  1. The plan with Channels
flowchart TB
A[Main Routine] <--> C{Channel}
subgraph Routines
D[Child Go Routine]
E[Child Go Routine]
F[Child Go Routine]
end
D <-.-> C
E <-.-> C
F <-.-> C

go-concurrency-parallelism's People

Contributors

arthur404dev avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.