(Translated by https://www.hiragana.jp/)
Java 5.0時代の非同期処理技術から学び直すScala/Java非同期処理 - Speaker Deck
Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Java 5.0時代じだい非同期ひどうき処理しょり技術ぎじゅつからまななおすScala/Java非同期ひどうき処理しょり

Java 5.0時代じだい非同期ひどうき処理しょり技術ぎじゅつからまななおすScala/Java非同期ひどうき処理しょり

More Decks by リチャード 真岡しんおか

Other Decks in Technology

Transcript

  1. Thread & Runnable public class MyRunnable implements Runnable { public

    void run() { ... } } Thread thread = new Thread(new MyRunnable()); thread.start();
  2. thread interference (atomic処理しょり) class Counter { private int c =

    0; public void increment() { c++; } public void decrement() { c--; } public int value() { return c; } }
  3. 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を上書うわがき)
  4. synchronized class Counter { private int c = 0; public

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

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

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

    locks • java.util.concurrency ツール ◦ locks, atomic, ExecutorService, thread-safe collections ◦ これらをうまく使つかうパターン、抽象ちゅうしょうさらたかいツールは後年こうねん Java 1.4 -> 5.0 時代じだいのまとめ
  8. 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
  9. 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)
  10. 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 (Java 5.0以前いぜんから使つかわれていた) https://docs.oracle.com/javase/tutorial/uiswing /concurrency/index.html
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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 コール
  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”); call stack event loop
  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. 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. 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. 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 コールバック タスクキュー
  21. Akka actorコードサンプル actor ! message class MyActor extends Actor {

    var state = ... def receive = { case MessageTypeX => ... case MessageTypeY => ... case MessageTypeZ => ... } }
  22. Oracle Concurrency guide https://docs.oracle.com/javase/tutorial/… Java 5.0やそれ以前いぜんからつづ非同期ひどうき処理しょり技術ぎじゅつくわしい。 Java Memory ModelとJava 5.0

    Oracle Concurrency in Swing guide https://docs.oracle.com/javase/tutorial/uiswing/... UIでのスレッド管理かんりプラクティスとして一部いちぶいま通用つうようする。
  23. 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をはじ様々さまざまがい ねんやコンパイラ最適さいてき影響えいきょうなどが解説かいせつされている。
  24. 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を題材だいざい動画どうが解説かいせつ している。
  25. GUIとThread 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でかれている。 YouTube: What the heck is the event loop anyway? JSConf https://youtu.be/8aGhZQkoFbQ アニメーションを駆使くししたevent loop解説かいせつ。100まん再生さいせい以上いじょう
  26. MDN web docs: Concurrency model and Event Loop https://developer.mozilla.org/en-US/docs/... MozillaによるEvent

    Loop解説かいせつ Akka docs: https://akka.io/docs/ 公式こうしきのドキュメント edX: Programming Reactive Systems https://www.edx.org/... AkkaのメンテナであったKonrad Malawski中心ちゅうしんとなって つくったオンライン学習がくしゅうコース Akkaとactor model
  27. YouTube: The Making of an IO - Daniel Spiewak https://youtu.be/g_jP47HFpWA

    関数かんすうがたプログラムと非同期ひどうき処理しょり関係かんけい説明せつめい。Cats Effectの思想しそう やスレッドプール使つかけベストプラクティスにもれる