Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go & Concurrency

KMKLabs
June 16, 2016

Go & Concurrency

Go adalah bahasa pemograman yang mudah digunakan untuk membuat aplikasi yang simpel dan efisien.

Go sendiri merupakan bahasa pemograman yang dibuat oleh salah satu tim di Google dan berbagai kontributor dari dunia open source.

Go merupakan bahasa pemograman yang perlu dicompile, seperti C dan C++, menggunakan static typing, garbage collection, dan fitur unik yang didesain untuk konkurensi.

Pada slide ini akan menjelaskan dasar tentang konkurensi dan pengaplikasiannya menggunakan Go lang.

KMKLabs

June 16, 2016
Tweet

More Decks by KMKLabs

Other Decks in Technology

Transcript

  1. About GO Go is an open source programming language that

    makes it easy to build simple, reliable, and efficient software. Go is an open source project developed by a team at Google and many contributors from the open source community. Go is a compiled programming language in the tradition of C and C++, with static typing, garbage collection, and unique language features enabling concurrent programming
  2. Package Go projects are grouped by package, which consists of

    one folder with many .go files. All of them will be compiled into 1 executable.
  3. Visibility func and type that has been defined with Capital

    letter is publicly visible Visible: func DoThis() { } Not Visible: func doThis() { }
  4. Interesting Features - Variables can use utf-8 characters var test

    = “Super” var 你怎么样 = “Apa Kabar” - Support for multiple return values from function func someFunc() (int, int) { return 7, 10 }
  5. Why concurrency? Look around you. What do you see? Do

    you see a single-stepping world doing one thing at a time? Or do you see a complex world of interacting, independently behaving pieces? That's why. Sequential processing on its own does not model the world's behavior.
  6. Concurrency is not parallelism Says everyone else talking about concurrency

    Concurrency is not parallelism, although it enables parallelism. Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once. Concurrency is about structure, parallelism is about execution.
  7. Example Move a pile of obsolete language manuals to the

    incinerator. With only one gopher this will take too long.
  8. More gophers and more carts This will go faster, but

    there will be bottlenecks at the pile and incinerator. Also need to synchronize the gophers. A message (that is, a communication between the gophers) will do.
  9. Another Design Three gophers in action, but with likely delays.

    Each gopher is an independently executing procedure, plus coordination (communication).
  10. Finer-grained concurrency Add another gopher procedure to return the empty

    carts. Four gophers in action for better flow, each doing one simple task. If we arrange everything right (implausible but not impossible), that's four times faster than our original one-gopher design.
  11. Another Design Here's another way to structure the problem as

    the concurrent composition of gopher procedures. Two gopher procedures, plus a staging pile.
  12. What is a goroutine? It's an independently executing function, launched

    by a go statement. It has its own call stack, which grows and shrinks as required. It's very cheap. It's practical to have thousands, even hundreds of thousands of goroutines. It's not a thread. There might be only one thread in a program with thousands of goroutines. Goroutine
  13. Channels Channels serve to synchronize execution of concurrently running functions

    and to provide a mechanism for their communication by passing a value of a specified type. It is about sending and receiving messages between goroutines.
  14. There are many ways to break the processing down. That's

    concurrent design. Once we have the breakdown, parallelization can fall out and correctness is easy. Summary
  15. The Problem Goal Serve bubur to a lines of 10

    customers in the busy morning in front of Ratu Plaza. Tasks 1. Siapkan dan jejerkan kotak styrofoam 2. Masukkan bubur dari panci ke kotak 3. Tanyakan topping yang diinginkan dan taburkan diatas bubur 4. Bungkus, minta bayaran dan serahkan pada pembeli
  16. Delegation & Timing 1. Buburman → Deretkan kotak (1 detik)

    2. Buburman → Masukkan bubur (2 detik) 3. Buburman → Topping (3 detik) 4. Buburman → Bayaran dan bungkus (1 detik)
  17. Using Sequential Design 1. Buburman Satu → Deretkan kotak (1

    detik) 2. Buburman Satu → Masukkan bubur (2 detik) 3. Buburman Satu → Topping (3 detik) 4. Buburman Satu → Bayaran dan bungkus (1 detik) 5. Kembali ke 1 1 Workers 7 detik / Customer Total 70 detik
  18. Using Concurrent Design 1. Buburman Satu → Deretkan kotak (1

    detik) 2. Buburman Dua → Masukkan bubur (2 detik) 3. Buburman Tiga → Topping (3 detik) 4. Buburman Empat → Bayaran dan bungkus (1 detik)
  19. Using Concurrent Design 1. Buburman Satu → Deretkan kotak (1

    detik) 2. Buburman Dua → Masukkan bubur (2 detik) 3. Buburman Tiga → Topping (3 detik) 4. Buburman Empat → Bayaran dan bungkus (1 detik) 4 Workers 7 detik / bubur Total 34 detik
  20. Computers are good at following instructions, but not at reading

    your mind. Donald Knuth American computer scientist, mathematician, and professor emeritus at Stanford University