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

非同期処理の歴史から見たコンピューティングの進化

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

 非同期処理の歴史から見たコンピューティングの進化

Avatar for リチャード 伊真岡

リチャード 伊真岡

August 30, 2019
Tweet

More Decks by リチャード 伊真岡

Other Decks in Technology

Transcript

  1. JavaScript: promise, async/await Go: goroutine, channel Kotlin: Coroutine Java: java.util.concurrent

    (1.5, 8.0) Scala: Future ReactiveX チュートリアルを見れば それっぽい物は書ける...
  2. Thread & Runnable public class MyRunnable implements Runnable { public

    void run() { ... } } Thread thread = new Thread(new MyRunnable()); thread.start();
  3. A Swing programmer deals with the following kinds of threads:

    • Initial threads, the threads that execute initial application code. • The event dispatch thread, where all event-handling code is executed. Most code that interacts with the Swing framework must also execute on this thread. • Worker threads, also known as background threads, where time-consuming background tasks are executed. スレッド使用例 Swing guide UI framework https://docs.oracle.com/javase/tutorial/uiswing /concurrency/index.html
  4. thread interference (非atomic処理) class Counter { private int c =

    0; public void increment() { c++; } public void decrement() { c--; } public int value() { return c; } }
  5. 1. Thread A: int c 取得 2. Thread B: int

    c 取得 3. Thread A: 取得した値をIncrement 4. Thread B: 取得した値をDecrement 5. Thread A: cに1を書き込み 6. Thread B: cに-1を書き込み (6が5を上書き)
  6. synchronized class Counter { private int c = 0; public

    synchronized void increment() { c++; } public synchronized void decrement() { c--; } public int value() { return c; } }
  7. volatile “write to a volatile variable establishes a happens-before relationship

    with subsequent reads of that same variable” volatile int counter = 0;
  8. atomic java.util.concurrent.atomic AtomicInteger AtomicBoolean AtomicLong AtomicReference ... //CAS operation boolean

    compareAndSet(expectedValue, updateValue); CASはCPUでサポートされた操作 java.util.concurrent.atomicはそれを利用する
  9. • Java Memory Model 仕様 ◦ happens-before ◦ volatile, synchonized,

    locks • java.util.concurrency ツール ◦ locks, atomic, ExecutorService, thread-safe collections ◦ これらをうまく使うパターン、抽象度の更に高いツールは後年 Java 1.4 -> 5.0 時代のまとめ
  10. CPUとキャッシュ Stack Exchange - Where exactly L1, L2 and L3

    Caches located in computer? https://superuser.com/questions/1961 43/where-exactly-l1-l2-and-l3-caches-loc ated-in-computer キャッシュは「スレッド ごとのメモリ空間」に関 わる -> data race -> happens-before
  11. CPUとキャッシュ Stack Exchange - Where exactly L1, L2 and L3

    Caches located in computer? https://superuser.com/questions/1961 43/where-exactly-l1-l2-and-l3-caches-loc ated-in-computer キャッシュは「スレッド ごとのメモリ空間」に関 わる -> data race -> happens-before data race (memory-consistency error)
  12. main() function funC(c) { … //make an ajax call }

    function funcB(b) { return funcC(b); } function funcA(a) { return funcB(a); } funcA(“my user data string”); call stack event loop
  13. funcA() main() function funC(c) { … //make an ajax call

    } function funcB(b) { return funcC(b); } function funcA(a) { return funcB(a); } funcA(“my user data string”); call stack event loop
  14. funcB() funcA() main() function funC(c) { … //make an ajax

    call } function funcB(b) { return funcC(c); } function funcA(a) { return funcB(a); } funcA(“my user data string”); call stack event loop
  15. funcB() funcA() main() function funC(c) { … //make an ajax

    call } function funcB(b) { return funcC(b); } function funcA(a) { return funcB(a); } funcA(“my user data string”); funcC() call stack event loop
  16. funcB() funcA() main() function funC(c) { … //make an ajax

    call } function funcB(b) { return funcC(c); } function funcA(a) { return funcB(a); } funcA(“my user data string”); funcC() call stack event loop ajax コール
  17. funcB() funcA() main() function funC(c) { … //make an ajax

    call } function funcB(b) { return funcC(c); } function funcA(a) { return funcB(a); } funcA(“my user data string”); call stack event loop
  18. funcB() funcA() main() function funC(c) { … //make an ajax

    call } function funcB(b) { return funcC(c); } function funcA(a) { return funcB(a); } funcA(“my user data string”); call stack event loop
  19. funcA() main() function funC(c) { … //make an ajax call

    } function funcB(b) { return funcC(c); } function funcA(a) { return funcB(a); } funcA(“my user data string”); call stack event loop
  20. main() function funC(c) { … //make an ajax call }

    function funcB(b) { return funcC(c); } function funcA(a) { return funcB(a); } funcA(“my user data string”); call stack event loop
  21. function funC(c) { … //make an ajax call } function

    funcB(b) { return funcC(c); } function funcA(a) { return funcB(a); } funcA(“my user data string”); event loop ajax コールバック タスクキュー
  22. JavaScript promise, async/await まとめ • Callback = 細分化された非同期処理 • 非同期処理のChaining

    ◦ ReactiveXなどのストリーム処理にも通じる • 「書きやすさ」という進化の方向性
  23. Akka actorコードサンプル actor ! message class MyActor extends Actor {

    var state = ... def receive = { case MessageTypeX => ... case MessageTypeY => ... case MessageTypeZ => ... } }
  24. Oracle Concurrency guide https://docs.oracle.com/javase/tutorial/… Java 5.0やそれ以前から続く非同期処理技術に詳しい。 Computer Hope processor history

    https://www.computerhope.com/history/processor.htm AMDが2005年に初のdual-coreを発売など。 Java Memory ModelとJava 5.0 Oracle Concurrency in Swing guide https://docs.oracle.com/javase/tutorial/uiswing/... UIでのスレッド管理プラクティスとして一部今も通用する。
  25. The JSR-133 Cookbook for Compiler Writers  http://gee.cs.oswego.edu/dl/jmm/cookbook.html JSR-133に深く関わったDoug Lea教授による情報。 The

    Java Memory Model  http://www.cs.umd.edu/~pugh/java/memoryModel/ JSR-133仕様書に載っていない周辺情報。 Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms https://www.cs.rochester.. ConcurrentLinkedQueue実装に使われたアルゴリズム。 JSR-133 https://www.cs.umd.edu/~pugh/… Java Memory Model仕様書。happens-beforeを始め様々な概 念やコンパイラ最適化の影響などが解説されている。
  26. OSとCPU Systems Performance https://www.amazon.com/gp… Netflixのパフォーマンス分析エンジニアBrendan Gregg氏に よる著書。CPUとスレッドについての章がある。 Brendan Gregg’s Home

    page http://www.brendangregg.com/overview.html Brendan Gregg氏の著書にも関わる様々な情報がある See How a CPU works https://youtu.be/cNN_tTXABUA Scott CPUという教育用の仮想的なCPUを題材に動画で解説 している。
  27. AjaxとGoogle Maps YouTube: Screen record of Google Maps in 2005

    https://youtu.be/PDG6tELjCOE 一般ユーザによる録画と思われる。 YouTube: What the heck is the event loop anyway? JSConf https://youtu.be/8aGhZQkoFbQ アニメーションを駆使したevent loop解説。100万再生以上。
  28. C10k problem ゆううきブログ: 2015年Webサーバアーキテクチャ序論 https://youtu.be/PDG6tELjCOE 現さくらインターネット研究所の坪内佑樹さんのブログ。C10k 問題や当時のサーバアーキテクチャを画像とともに解説。 The Node.js Event

    Loop, Timers, and process.nextTick() https://nodejs.org/uk/docs/guides/... 公式ドキュメント。各フェーズ毎の詳細な解説。 GitHub: libuv https://github.com/libuv/libuv/... node.js内部で使われているイベントループの実装を含む非同 期I/Oライブラリ。Cで書かれている。
  29. Inside NGINX: How We Designed for Performance & Scale https://www.nginx.com/blog/...

    nginx公式のアーキテクチャ解説。 MDN web docs: Concurrency model and Event Loop https://developer.mozilla.org/en-US/docs/... MozillaによるEvent Loop解説 JavaScript Promiseの本 https://azu.github.io/promises-book/ JavaScriptを中心に著名なエンジニアであるazu氏が書いた Promiseおよびasync/awaitを解説したウェブ上の本 JavaScript promise, async/await
  30. Akka docs: https://akka.io/docs/ 公式のドキュメント edX: Programming Reactive Systems https://www.edx.org/... AkkaのメンテナであったKonrad

    Malawski氏が中心となって 作ったオンライン学習コース Scala: Futures and Promises https://docs.scala-lang.org/overviews/core/futures.html Scala公式のFutureのドキュメント。Chainingの参考になる。 Akkaとactor model
  31. YouTube: The Making of an IO - Daniel Spiewak https://youtu.be/g_jP47HFpWA

    関数型プログラムと非同期処理の関係を説明。Cats Effectの思想 やスレッドプール使い分けベストプラクティスにも触れる