├── .nojekyll ├── 00~并发导论 ├── 02~并发的挑战 │ └── 上下文切换.md └── 01~并发的特性 │ └── 并发级别.md ├── 01~并发基础 ├── 线程安全 │ ├── 并发容器 │ │ └── README.md │ ├── 锁 │ │ └── 互斥锁 │ │ │ ├── 读写锁.md │ │ │ └── 锁优化.md │ └── 同步器 │ │ └── TencentOS 中信号量的实现.md ├── .DS_Store ├── 并发单元 │ ├── README.md │ ├── 进程 │ │ └── README.md │ └── 线程 │ │ ├── 线程模型 │ │ └── README.md │ │ └── 线程池 │ │ └── README.md └── 01~内存模型 │ ├── 01~可见性与缓存一致性 │ └── README.md │ └── 03~原子性 │ └── 03~原子性.md ├── INTRODUCTION.md ├── 99~参考资料 └── 2013~《七周七并发模型》 │ ├── 01~概述.md │ ├── 02~线程与锁.md │ ├── 07~数据并行.md │ ├── 03~函数式编程.md │ ├── 05~Actor.md │ ├── 08~Lambda 架构.md │ ├── 06~CSP 通信顺序进程.md │ ├── 04~Clojure 之道:分离标识与状态.md │ ├── README.md │ └── codes │ ├── CSP │ ├── Wizard │ │ ├── .gitignore │ │ ├── resources │ │ │ └── public │ │ │ │ └── styles.css │ │ ├── src-clj │ │ │ └── wizard │ │ │ │ └── core.clj │ │ └── project.clj │ ├── Animation │ │ ├── .gitignore │ │ ├── resources │ │ │ └── public │ │ │ │ └── index.html │ │ ├── src-clj │ │ │ └── animation │ │ │ │ └── core.clj │ │ └── project.clj │ ├── HelloClojureScript │ │ ├── .gitignore │ │ ├── resources │ │ │ └── public │ │ │ │ └── index.html │ │ ├── src-clj │ │ │ └── hello_clojurescript │ │ │ │ └── core.clj │ │ ├── project.clj │ │ └── src-cljs │ │ │ └── hello_clojurescript │ │ │ └── core.cljs │ ├── README │ ├── WordCount │ │ ├── src │ │ │ └── wordcount │ │ │ │ ├── words.clj │ │ │ │ ├── polling.clj │ │ │ │ └── http.clj │ │ └── project.clj │ ├── Sieve │ │ ├── project.clj │ │ └── src │ │ │ └── sieve │ │ │ └── core.clj │ ├── SieveTimeout │ │ ├── project.clj │ │ └── src │ │ │ └── sieve │ │ │ └── core.clj │ ├── Polling │ │ ├── project.clj │ │ └── src │ │ │ └── polling │ │ │ └── core.clj │ └── channels │ │ ├── project.clj │ │ └── src │ │ └── channels │ │ └── core.clj │ ├── LambdaArchitecture │ ├── WordCount │ │ └── input │ │ │ ├── file1.txt │ │ │ └── file2.txt │ ├── ExtractWikiText │ │ ├── project.clj │ │ └── src │ │ │ └── wiki │ │ │ ├── core.clj │ │ │ └── pages.clj │ └── ExtractWikiContributors │ │ ├── project.clj │ │ └── src │ │ └── wiki │ │ └── core.clj │ ├── DataParallelism │ ├── Ripple │ │ └── run │ ├── Zoom │ │ ├── run │ │ └── src │ │ │ └── main │ │ │ └── resources │ │ │ └── zoom.cl │ ├── FindDevices │ │ └── Makefile │ ├── FindMinimumOneWorkGroup │ │ ├── Makefile │ │ └── find_minimum.cl │ ├── MultiplyArrays │ │ ├── Makefile │ │ └── multiply_arrays.cl │ ├── FindMinimumMultipleWorkGroups │ │ ├── Makefile │ │ └── find_minimum.cl │ ├── MultiplyArraysProfiled │ │ ├── Makefile │ │ └── multiply_arrays.cl │ ├── MultiplyArraysWithErrorHandling │ │ ├── Makefile │ │ └── multiply_arrays.cl │ ├── MatrixMultiplication │ │ ├── Makefile │ │ └── matrix_multiplication.cl │ ├── MatrixMultiplicationProfiled │ │ ├── Makefile │ │ └── matrix_multiplication.cl │ └── README │ ├── ActorsScala │ ├── HelloActors │ │ ├── project │ │ │ └── plugins.sbt │ │ ├── build.sbt │ │ └── src │ │ │ └── main │ │ │ └── scala │ │ │ └── com │ │ │ └── paulbutcher │ │ │ └── HelloActors.scala │ ├── WordCount │ │ ├── project │ │ │ └── plugins.sbt │ │ ├── build.sbt │ │ └── src │ │ │ └── main │ │ │ └── scala │ │ │ └── com │ │ │ └── paulbutcher │ │ │ ├── WordCount.scala │ │ │ ├── Counter.scala │ │ │ ├── Parser.scala │ │ │ ├── Master.scala │ │ │ └── Words.scala │ ├── HelloActorsBetter │ │ ├── project │ │ │ └── plugins.sbt │ │ └── build.sbt │ ├── WordCountFaultTolerant │ │ ├── project │ │ │ └── plugins.sbt │ │ ├── src │ │ │ └── main │ │ │ │ ├── resources │ │ │ │ └── application.conf │ │ │ │ └── scala │ │ │ │ └── com │ │ │ │ └── paulbutcher │ │ │ │ ├── Words.scala │ │ │ │ ├── Accumulator.scala │ │ │ │ └── Counter.scala │ │ └── build.sbt │ ├── WordCountMultipleCounters │ │ ├── project │ │ │ └── plugins.sbt │ │ ├── build.sbt │ │ └── src │ │ │ └── main │ │ │ └── scala │ │ │ └── com │ │ │ └── paulbutcher │ │ │ ├── WordCount.scala │ │ │ ├── Parser.scala │ │ │ ├── Counter.scala │ │ │ ├── Accumulator.scala │ │ │ ├── Words.scala │ │ │ └── Master.scala │ ├── README │ ├── Paths │ │ ├── build.sbt │ │ └── src │ │ │ └── main │ │ │ └── scala │ │ │ └── com │ │ │ └── paulbutcher │ │ │ └── TestActor.scala │ ├── HelloCluster │ │ ├── src │ │ │ └── main │ │ │ │ ├── resources │ │ │ │ └── application.conf │ │ │ │ └── scala │ │ │ │ └── com │ │ │ │ └── paulbutcher │ │ │ │ └── TestActor.scala │ │ └── build.sbt │ ├── Lifecycle │ │ ├── build.sbt │ │ └── src │ │ │ └── main │ │ │ └── scala │ │ │ └── com │ │ │ └── paulbutcher │ │ │ └── TestActor.scala │ └── BuggyCache │ │ ├── build.sbt │ │ └── src │ │ └── main │ │ └── scala │ │ └── com │ │ └── paulbutcher │ │ ├── BuggyCache.scala │ │ └── Master.scala │ ├── threading-essentials-course │ ├── .gitignore │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── com │ │ │ └── islomar │ │ │ ├── CounterClient.java │ │ │ ├── ExecutorServiceExample.java │ │ │ ├── Counter.java │ │ │ ├── NewThreadExample.java │ │ │ └── ParallelSorting.java │ └── README.md │ ├── 01~ThreadsLocks │ ├── README │ ├── HttpDownload │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── paulbutcher │ │ │ │ ├── ProgressListener.java │ │ │ │ └── HttpDownload.java │ │ └── pom.xml │ ├── HttpDownloadFixed │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── paulbutcher │ │ │ │ ├── ProgressListener.java │ │ │ │ └── HttpDownload.java │ │ └── pom.xml │ ├── HttpDownloadBetter │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── paulbutcher │ │ │ │ ├── ProgressListener.java │ │ │ │ └── HttpDownload.java │ │ └── pom.xml │ ├── WordCountConcurrentHashMap │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── paulbutcher │ │ │ │ ├── PoisonPill.java │ │ │ │ ├── Page.java │ │ │ │ ├── WikiPage.java │ │ │ │ └── Parser.java │ │ └── pom.xml │ ├── WordCountProducerConsumer │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── paulbutcher │ │ │ │ ├── PoisonPill.java │ │ │ │ ├── Page.java │ │ │ │ ├── WikiPage.java │ │ │ │ └── Parser.java │ │ └── pom.xml │ ├── WordCountBatchConcurrentHashMap │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── paulbutcher │ │ │ │ ├── PoisonPill.java │ │ │ │ ├── Page.java │ │ │ │ ├── WikiPage.java │ │ │ │ └── Parser.java │ │ └── pom.xml │ ├── WordCountSynchronizedHashMap │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── paulbutcher │ │ │ │ ├── PoisonPill.java │ │ │ │ ├── Page.java │ │ │ │ ├── WikiPage.java │ │ │ │ └── Parser.java │ │ └── pom.xml │ ├── DiningPhilosophers │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── paulbutcher │ │ │ │ ├── Chopstick.java │ │ │ │ └── DiningPhilosophers.java │ │ └── pom.xml │ ├── DiningPhilosophersFixed │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── paulbutcher │ │ │ │ ├── Chopstick.java │ │ │ │ └── DiningPhilosophers.java │ │ └── pom.xml │ ├── DiningPhilosophersTimeout │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── paulbutcher │ │ │ │ ├── Chopstick.java │ │ │ │ └── DiningPhilosophers.java │ │ └── pom.xml │ ├── WordCount │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── paulbutcher │ │ │ │ └── Page.java │ │ └── pom.xml │ ├── HelloWorld │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── paulbutcher │ │ │ │ └── HelloWorld.java │ │ └── pom.xml │ ├── Puzzle │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── com │ │ │ └── paulbutcher │ │ │ └── Puzzle.java │ ├── Counting │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── com │ │ │ └── paulbutcher │ │ │ └── Counting.java │ ├── EchoServer │ │ └── pom.xml │ ├── LinkedList │ │ └── pom.xml │ ├── CountingFixed │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── com │ │ │ └── paulbutcher │ │ │ └── Counting.java │ ├── CountingBetter │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── com │ │ │ └── paulbutcher │ │ │ └── Counting.java │ ├── Interruptible │ │ └── pom.xml │ ├── Uninterruptible │ │ └── pom.xml │ ├── EchoServerBetter │ │ └── pom.xml │ └── DiningPhilosophersCondition │ │ ├── pom.xml │ │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── paulbutcher │ │ └── DiningPhilosophers.java │ ├── Clojure │ ├── README │ ├── Transfer │ │ └── project.clj │ ├── Logger │ │ ├── project.clj │ │ └── src │ │ │ └── logger │ │ │ └── core.clj │ ├── DiningPhilosphersAtom │ │ └── project.clj │ ├── DiningPhilosphersSTM │ │ └── project.clj │ ├── DiningPhilosphersAtom2 │ │ ├── project.clj │ │ └── src │ │ │ └── philosophers │ │ │ └── util.clj │ ├── TranscriptTest │ │ ├── project.clj │ │ └── src │ │ │ └── test │ │ │ └── potato.clj │ ├── TranscriptHandler │ │ ├── src │ │ │ └── server │ │ │ │ ├── charset.clj │ │ │ │ ├── translate.clj │ │ │ │ └── sentences.clj │ │ └── project.clj │ └── TournamentServer │ │ ├── project.clj │ │ └── src │ │ └── server │ │ └── core.clj │ ├── FunctionalProgramming │ ├── README │ ├── Sum │ │ ├── project.clj │ │ └── src │ │ │ └── sum │ │ │ └── core.clj │ ├── WordCount │ │ ├── src │ │ │ └── wordcount │ │ │ │ ├── words.clj │ │ │ │ ├── word_frequencies.clj │ │ │ │ ├── core.clj │ │ │ │ └── pages.clj │ │ └── project.clj │ ├── WordCountReducer │ │ ├── src │ │ │ └── wordcount │ │ │ │ ├── words.clj │ │ │ │ ├── core.clj │ │ │ │ └── pages.clj │ │ └── project.clj │ ├── Reducers │ │ ├── project.clj │ │ └── src │ │ │ └── reducers │ │ │ └── parallel_frequencies.clj │ ├── TranscriptTest │ │ ├── project.clj │ │ └── src │ │ │ └── test │ │ │ └── core.clj │ ├── TranscriptHandler2 │ │ ├── src │ │ │ └── server │ │ │ │ ├── charset.clj │ │ │ │ └── sentences.clj │ │ └── project.clj │ ├── Translator │ │ └── project.clj │ ├── TranscriptHandler │ │ ├── project.clj │ │ └── src │ │ │ └── server │ │ │ └── core.clj │ └── DateFormatBug │ │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── com │ │ │ └── paulbutcher │ │ │ └── DateParser.java │ │ └── pom.xml │ └── Actors │ ├── README │ ├── patterns │ └── patterns.ex │ ├── word_count │ └── mix.exs │ ├── counter │ ├── counter.ex │ ├── counter2.ex │ └── counter3.ex │ ├── links │ └── links.ex │ ├── parallel │ └── parallel.ex │ └── hello_actors │ ├── hello_actors.exs │ └── hello_actors2.exs ├── 02~并发模型 ├── Actor │ ├── Communicating event-loops.md │ ├── Active Objects.md │ ├── 基于 Process 的 Actor 模型.md │ ├── 经典 Actor 模型.md │ └── README.md ├── 生产者与消费者 │ ├── Disruptor │ │ ├── 99~参考资料 │ │ │ ├── README.md │ │ │ ├── 2017~Disruptor 无锁缓存框架.md │ │ │ ├── 2016~美团技术团队~高性能队列 Disruptor.md │ │ │ └── 2020~初识 Disruptor 框架与广播模式.md │ │ ├── README.md │ │ └── API 使用 │ │ │ └── 99~参考资料 │ │ │ └── 2023~单机最快的队列 Disruptor 解析和使用.md │ └── README.md ├── 软件事务内存 │ └── README.md ├── .DS_Store ├── 主从模型 │ └── README.md ├── 流水线模型 │ └── 流水线模型.md └── 结构化并发 │ └── README.md ├── 03~并发网络 IO ├── 实践案例 │ └── 并发网络服务.md ├── .DS_Store ├── IO 模型 │ ├── 99~参考资料 │ │ └── 2021~深度长文:从 bio 到 nio 到 aio,再到响应式编程.md │ └── Reactor │ │ ├── Proactor.md │ │ └── README.md ├── README.md └── 非阻塞与异步.md ├── .DS_Store ├── 04~异步编程 ├── .DS_Store ├── 反应式编程 │ ├── 反压 │ │ ├── 0-老周聊架构-响应式流的核心机制:背压机制.md │ │ └── 反压.md │ └── README.md └── Futures & Promises │ ├── README.md │ └── Promise 实践.md ├── .gitattributes ├── 09~并发场景 └── 并发下载 │ └── 并发下载.md └── .gitignore /.nojekyll: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /00~并发导论/02~并发的挑战/上下文切换.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /01~并发基础/线程安全/并发容器/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /INTRODUCTION.md: -------------------------------------------------------------------------------- 1 | # 本篇导读 2 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/01~概述.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/02~线程与锁.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/07~数据并行.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /01~并发基础/线程安全/锁/互斥锁/读写锁.md: -------------------------------------------------------------------------------- 1 | # 读写锁 2 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/03~函数式编程.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/05~Actor.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/08~Lambda 架构.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /02~并发模型/Actor/Communicating event-loops.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /02~并发模型/生产者与消费者/Disruptor/99~参考资料/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /02~并发模型/生产者与消费者/README.md: -------------------------------------------------------------------------------- 1 | # 生产者与消费者 2 | -------------------------------------------------------------------------------- /02~并发模型/软件事务内存/README.md: -------------------------------------------------------------------------------- 1 | # 基于软件事务内存的并发 2 | -------------------------------------------------------------------------------- /03~并发网络 IO/实践案例/并发网络服务.md: -------------------------------------------------------------------------------- 1 | # 案例:并发网络服务 2 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/06~CSP 通信顺序进程.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/04~Clojure 之道:分离标识与状态.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/README.md: -------------------------------------------------------------------------------- 1 | # 七周七并发模型 2 | -------------------------------------------------------------------------------- /02~并发模型/Actor/Active Objects.md: -------------------------------------------------------------------------------- 1 | # Active Objects 2 | -------------------------------------------------------------------------------- /02~并发模型/Actor/基于 Process 的 Actor 模型.md: -------------------------------------------------------------------------------- 1 | # 基于 Process 的 Actor 模型 2 | -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wx-chevalier/Concurrent-Notes/master/.DS_Store -------------------------------------------------------------------------------- /01~并发基础/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wx-chevalier/Concurrent-Notes/master/01~并发基础/.DS_Store -------------------------------------------------------------------------------- /02~并发模型/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wx-chevalier/Concurrent-Notes/master/02~并发模型/.DS_Store -------------------------------------------------------------------------------- /04~异步编程/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wx-chevalier/Concurrent-Notes/master/04~异步编程/.DS_Store -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/Wizard/.gitignore: -------------------------------------------------------------------------------- 1 | resources/public/js/main.js 2 | out/ 3 | repl/ 4 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.xmind filter=lfs diff=lfs merge=lfs -text 2 | *.pdf filter=lfs diff=lfs merge=lfs -text 3 | -------------------------------------------------------------------------------- /03~并发网络 IO/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wx-chevalier/Concurrent-Notes/master/03~并发网络 IO/.DS_Store -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/Animation/.gitignore: -------------------------------------------------------------------------------- 1 | resources/public/js/main.js 2 | out/ 3 | repl/ 4 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/HelloClojureScript/.gitignore: -------------------------------------------------------------------------------- 1 | resources/public/js/main.js 2 | out/ 3 | repl/ 4 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/LambdaArchitecture/WordCount/input/file1.txt: -------------------------------------------------------------------------------- 1 | one potato two potato three potato four 2 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/LambdaArchitecture/WordCount/input/file2.txt: -------------------------------------------------------------------------------- 1 | five potato six potato seven potato more 2 | -------------------------------------------------------------------------------- /01~并发基础/并发单元/README.md: -------------------------------------------------------------------------------- 1 | # 并发单元:进程与线程 2 | 3 | # Links 4 | 5 | - http://www.zhihu.com/question/44087187 进程和线程之间有什么根本性的区别? -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/Ripple/run: -------------------------------------------------------------------------------- 1 | env MAVEN_OPTS=-Djava.library.path=target/natives mvn compile exec:java 2 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/Zoom/run: -------------------------------------------------------------------------------- 1 | env MAVEN_OPTS=-Djava.library.path=target/natives mvn compile exec:java 2 | -------------------------------------------------------------------------------- /01~并发基础/线程安全/同步器/TencentOS 中信号量的实现.md: -------------------------------------------------------------------------------- 1 | # TecentOS 中信号量的实现 2 | 3 | # Links 4 | 5 | - https://mp.weixin.qq.com/s/URIxdyAmmvqnGx9ZUVerVw 6 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/HelloActors/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.1") -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCount/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.1") -------------------------------------------------------------------------------- /04~异步编程/反应式编程/反压/0-老周聊架构-响应式流的核心机制:背压机制.md: -------------------------------------------------------------------------------- 1 | # 响应式流的核心机制——背压机制 2 | 3 | # Links 4 | 5 | - https://xie.infoq.cn/article/122dbe4d127a9a118273983e1 6 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/HelloActorsBetter/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.1") -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCountFaultTolerant/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.1") -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCountMultipleCounters/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.1") -------------------------------------------------------------------------------- /01~并发基础/线程安全/锁/互斥锁/锁优化.md: -------------------------------------------------------------------------------- 1 | # 锁优化 2 | 3 | 在多核时代,多线程对系统的性能提升有着巨大的作用,但是多线程对系统也相应的产生了额外的开销,线程本身的数据、线程的调度、线程的申请释放等都对系统的产生额外的负担,锁是在高并发的环境下为了保证数据的正确性而产生的同步手段,为了进一步提升性能,我们就需要对锁进行优化。 4 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/threading-essentials-course/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | *.class 3 | *.project 4 | *.settings 5 | *.classpath 6 | 7 | # IntelliJ files 8 | .idea/ 9 | *.iml -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/Wizard/resources/public/styles.css: -------------------------------------------------------------------------------- 1 | label { display:block; width:8em; clear:left; float:left; 2 | text-align:right; margin-right: 3pt; } 3 | input { display:block; } 4 | .step { display:none; } 5 | /* END_HIGHLIGHT */ -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/README: -------------------------------------------------------------------------------- 1 | These examples are all written in Java and compiled with Maven: 2 | 3 | http://maven.apache.org/download.cgi 4 | 5 | To build and run, change into the directory of the project and: 6 | 7 | mvn compile 8 | mvn exec:java 9 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/FindDevices/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname -s),Darwin) 2 | LIBS=-framework OpenCL 3 | else 4 | LIBS=-lOpenCL 5 | endif 6 | 7 | target/find_devices: find_devices.c 8 | mkdir -p target 9 | gcc -std=c99 find_devices.c $(LIBS) -o target/find_devices 10 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/FindMinimumOneWorkGroup/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname -s),Darwin) 2 | LIBS=-framework OpenCL 3 | else 4 | LIBS=-lOpenCL 5 | endif 6 | 7 | target/find_minimum: find_minimum.c 8 | mkdir -p target 9 | gcc -std=c99 find_minimum.c $(LIBS) -o target/find_minimum 10 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/MultiplyArrays/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname -s),Darwin) 2 | LIBS=-framework OpenCL 3 | else 4 | LIBS=-lOpenCL 5 | endif 6 | 7 | target/multiply_arrays: multiply_arrays.c 8 | mkdir -p target 9 | gcc -std=c99 multiply_arrays.c $(LIBS) -o target/multiply_arrays 10 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/FindMinimumMultipleWorkGroups/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname -s),Darwin) 2 | LIBS=-framework OpenCL 3 | else 4 | LIBS=-lOpenCL 5 | endif 6 | 7 | target/find_minimum: find_minimum.c 8 | mkdir -p target 9 | gcc -std=c99 find_minimum.c $(LIBS) -o target/find_minimum 10 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/MultiplyArraysProfiled/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname -s),Darwin) 2 | LIBS=-framework OpenCL 3 | else 4 | LIBS=-lOpenCL 5 | endif 6 | 7 | target/multiply_arrays: multiply_arrays.c 8 | mkdir -p target 9 | gcc -std=c99 multiply_arrays.c $(LIBS) -o target/multiply_arrays 10 | -------------------------------------------------------------------------------- /03~并发网络 IO/IO 模型/99~参考资料/2021~深度长文:从 bio 到 nio 到 aio,再到响应式编程.md: -------------------------------------------------------------------------------- 1 | # Links 2 | 3 | - 深度长文:从bio到nio到aio,再到响应式编程 https://mp.weixin.qq.com/s?__biz=MzA4MTc4NTUxNQ==&mid=2650524542&idx=1&sn=dc32eef9e033be79ad122a2ccbe88c2b&chksm=8780ccbab0f745ac325f0ab4351e1e6cfab8ab986e983cdc77a4df73dc8a18ae59b5888cd81b&token=1109061299&lang=zh_CN#rd -------------------------------------------------------------------------------- /04~异步编程/反应式编程/README.md: -------------------------------------------------------------------------------- 1 | # 反应式编程 2 | 3 | 反应式编程是一种以异步处理数据流为中心思想的编程范式,这个范式存在已久,不是新概念,就像面向过程、面向对象编程、函数式编程等范式。Reactive 模型最核心的是线程和消息管道。线程用于侦听事件,消息管道用于线程之间通信不同的消息。在异步编程模式中,我们描述了两种获得上一个任务执行结果的方式: 4 | 5 | - 一个就是主动轮询,我们把它称为 Proactive 方式; 6 | - 另一个就是被动接收反馈,我们称为 Reactive 方式。 7 | 8 | 简单来说,在 Reactive 方式中,上一个任务执行结果的反馈就是一个事件,这个事件的到来会触发下一个任务的执行。 9 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/MultiplyArraysWithErrorHandling/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname -s),Darwin) 2 | LIBS=-framework OpenCL 3 | else 4 | LIBS=-lOpenCL 5 | endif 6 | 7 | target/multiply_arrays: multiply_arrays.c 8 | mkdir -p target 9 | gcc -std=c99 multiply_arrays.c $(LIBS) -o target/multiply_arrays 10 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/MatrixMultiplication/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname -s),Darwin) 2 | LIBS=-framework OpenCL 3 | else 4 | LIBS=-lOpenCL 5 | endif 6 | 7 | target/matrix_multiplication: matrix_multiplication.c 8 | mkdir -p target 9 | gcc -std=c99 matrix_multiplication.c $(LIBS) -o target/matrix_multiplication 10 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/HelloActors/build.sbt: -------------------------------------------------------------------------------- 1 | organization := "com.paulbutcher" 2 | 3 | name := "actors-hello-actors" 4 | 5 | version := "1.0" 6 | 7 | scalaVersion := "2.10.0" 8 | 9 | scalacOptions ++= Seq("-deprecation", "-unchecked", "-feature") 10 | 11 | libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.1.0" 12 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/MatrixMultiplicationProfiled/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(shell uname -s),Darwin) 2 | LIBS=-framework OpenCL 3 | else 4 | LIBS=-lOpenCL 5 | endif 6 | 7 | target/matrix_multiplication: matrix_multiplication.c 8 | mkdir -p target 9 | gcc -std=c99 matrix_multiplication.c $(LIBS) -o target/matrix_multiplication 10 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/README: -------------------------------------------------------------------------------- 1 | These examples are all written in Scala and compiled with sbt: 2 | 3 | http://www.scala-sbt.org 4 | 5 | To build and run, change into the directory of the project and: 6 | 7 | sbt run 8 | 9 | To run an interactive console (REPL) with the code for a project loaded: 10 | 11 | sbt console 12 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Clojure/README: -------------------------------------------------------------------------------- 1 | These examples are all written in Clojure and compiled with Leiningen: 2 | 3 | http://leiningen.org 4 | 5 | To build and run, change into the directory of the project and: 6 | 7 | lein run 8 | 9 | To run an interactive console (REPL) with the code for a project loaded: 10 | 11 | lein repl 12 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCount/build.sbt: -------------------------------------------------------------------------------- 1 | organization := "com.paulbutcher" 2 | 3 | name := "actors-word-count-flow-control" 4 | 5 | version := "1.0" 6 | 7 | scalaVersion := "2.10.0" 8 | 9 | scalacOptions ++= Seq("-deprecation", "-unchecked", "-feature") 10 | 11 | libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.1.0" 12 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/HelloActorsBetter/build.sbt: -------------------------------------------------------------------------------- 1 | organization := "com.paulbutcher" 2 | 3 | name := "actors-hello-actors-better" 4 | 5 | version := "1.0" 6 | 7 | scalaVersion := "2.10.0" 8 | 9 | scalacOptions ++= Seq("-deprecation", "-unchecked", "-feature") 10 | 11 | libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.1.0" 12 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/README: -------------------------------------------------------------------------------- 1 | These examples are all written in Clojure and compiled with Leiningen: 2 | 3 | http://leiningen.org 4 | 5 | To build and run, change into the directory of the project and: 6 | 7 | lein run 8 | 9 | To run an interactive console (REPL) with the code for a project loaded: 10 | 11 | lein repl 12 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Actors/README: -------------------------------------------------------------------------------- 1 | These examples are all written in Elixir 2 | 3 | To run a script (extension .exs): 4 | 5 | elixir filename.exs 6 | 7 | To run Interactive Elixir with the definitions in a file loaded: 8 | 9 | iex filename.ex 10 | 11 | To run Interactive Elixir for a project with mix.exs in the root: 12 | 13 | iex -S mix 14 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCountMultipleCounters/build.sbt: -------------------------------------------------------------------------------- 1 | organization := "com.paulbutcher" 2 | 3 | name := "actors-word-count-multiple-counters" 4 | 5 | version := "1.0" 6 | 7 | scalaVersion := "2.10.0" 8 | 9 | scalacOptions ++= Seq("-deprecation", "-unchecked", "-feature") 10 | 11 | libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.1.0" 12 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/README: -------------------------------------------------------------------------------- 1 | These examples are all written in Clojure and compiled with Leiningen: 2 | 3 | http://leiningen.org 4 | 5 | To build and run, change into the directory of the project and: 6 | 7 | lein run 8 | 9 | To run an interactive console (REPL) with the code for a project loaded: 10 | 11 | lein repl 12 | 13 | To compile ClojureScript: 14 | 15 | lein cljsbuild once 16 | -------------------------------------------------------------------------------- /02~并发模型/生产者与消费者/Disruptor/README.md: -------------------------------------------------------------------------------- 1 | # Disruptor 2 | 3 | Disruptor 是英国外汇交易公司 LMAX 开发的一个高性能队列,研发的初衷是解决内存队列的延迟问题(在性能测试中发现竟然与 I/O 操作处于同样的数量级)。基于 Disruptor 开发的系统单线程能支撑每秒 600 万订单,2010 年在 QCon 演讲后,获得了业界关注。2011 年,企业应用软件专家 Martin Fowler 专门撰写长文介绍。同年它还获得了 Oracle 官方的 Duke 大奖。 4 | 5 | Disruptor 是一个高性能的异步处理框架,或者可以认为是最快的消息框架(轻量的 JMS),也可以认为是一个观察者模式的实现,或者事件监听模式的实现。目前,包括 Apache Storm、Camel、Log4j 2 在内的很多知名项目都应用了 Disruptor 以获取高性能。 6 | 7 | # Links 8 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/Paths/build.sbt: -------------------------------------------------------------------------------- 1 | organization := "com.paulbutcher" 2 | 3 | name := "actors-paths" 4 | 5 | version := "1.0" 6 | 7 | scalaVersion := "2.10.0" 8 | 9 | scalacOptions ++= Seq("-deprecation", "-unchecked", "-feature") 10 | 11 | libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.1.0" 12 | 13 | initialCommands in console := """ 14 | import akka.actor._ 15 | import com.paulbutcher._ 16 | """ -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCountFaultTolerant/src/main/resources/application.conf: -------------------------------------------------------------------------------- 1 | akka { 2 | actor { 3 | provider = "akka.cluster.ClusterActorRefProvider" 4 | } 5 | remote { 6 | transport = "akka.remote.netty.NettyRemoteTransport" 7 | log-remote-lifecycle-events = off 8 | } 9 | 10 | extensions = ["akka.cluster.Cluster"] 11 | 12 | cluster { 13 | auto-join = off 14 | auto-down = on 15 | } 16 | } -------------------------------------------------------------------------------- /09~并发场景/并发下载/并发下载.md: -------------------------------------------------------------------------------- 1 | # 并发下载 2 | 3 | 首先如果你真的在万兆网当中测试过的话,会发现只要配置得当,一个TCP连接也完全可以将一张万兆网卡的带宽占满了。所以“多线程下载一个大文件的速度更快”这句话本质上来说就是错误的。那为什么真实场景下用多线程下载似乎会快一些呢? 4 | 5 | - 延迟高的情况下,一个TCP连接要占满线路带宽,需要有足够大的TCP window,通常超过默认的TCP window上限,需要通过TCP window scale扩展来增加window大小。当年的操作系统对window scale的支持不太好,增加TCP连接数相当于变相增大window。 6 | 7 | - 操作系统的TCP流控实现不太合理的时候,遇到丢包可能速度会掉得很快,多个连接可以缓解这个问题,至少看到速度比较平缓,不会一下掉一半 8 | 9 | - 某些网站限制了单个TCP连接的速度,或者本身因为实现问题单个TCP连接就有性能上限(比如错误地使用了很小的buffer来做IO),多个连接也就可以增加性能。 -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/HelloCluster/src/main/resources/application.conf: -------------------------------------------------------------------------------- 1 | akka { 2 | loglevel = ERROR 3 | actor { 4 | provider = "akka.cluster.ClusterActorRefProvider" 5 | } 6 | remote { 7 | transport = "akka.remote.netty.NettyRemoteTransport" 8 | log-remote-lifecycle-events = off 9 | } 10 | 11 | extensions = ["akka.cluster.Cluster"] 12 | 13 | cluster { 14 | auto-join = off 15 | auto-down = on 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/HelloCluster/build.sbt: -------------------------------------------------------------------------------- 1 | organization := "com.paulbutcher" 2 | 3 | name := "actors-hello-cluster" 4 | 5 | version := "1.0" 6 | 7 | scalaVersion := "2.10.0" 8 | 9 | scalacOptions ++= Seq("-deprecation", "-unchecked", "-feature") 10 | 11 | libraryDependencies ++= Seq( 12 | "com.typesafe.akka" %% "akka-actor" % "2.1.0", 13 | "com.typesafe.akka" %% "akka-cluster-experimental" % "2.1.0", 14 | "com.github.scopt" %% "scopt" % "2.1.0") 15 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/Lifecycle/build.sbt: -------------------------------------------------------------------------------- 1 | organization := "com.paulbutcher" 2 | 3 | name := "actors-lifecycle" 4 | 5 | version := "1.0" 6 | 7 | scalaVersion := "2.10.0" 8 | 9 | scalacOptions ++= Seq("-deprecation", "-unchecked", "-feature") 10 | 11 | libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.1.0" 12 | 13 | initialCommands in console := """ 14 | import akka.actor._ 15 | import com.paulbutcher._ 16 | val system = ActorSystem("Paths") 17 | """ -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCountFaultTolerant/build.sbt: -------------------------------------------------------------------------------- 1 | organization := "com.paulbutcher" 2 | 3 | name := "actors-word-count-fault-tolerant" 4 | 5 | version := "1.0" 6 | 7 | scalaVersion := "2.10.0" 8 | 9 | scalacOptions ++= Seq("-deprecation", "-unchecked", "-feature") 10 | 11 | libraryDependencies ++= Seq( 12 | "com.typesafe.akka" %% "akka-actor" % "2.1.0", 13 | "com.typesafe.akka" %% "akka-cluster-experimental" % "2.1.0", 14 | "com.github.scopt" %% "scopt" % "2.1.0") 15 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/BuggyCache/build.sbt: -------------------------------------------------------------------------------- 1 | organization := "com.paulbutcher" 2 | 3 | name := "actors-buggy-cache" 4 | 5 | version := "1.0" 6 | 7 | scalaVersion := "2.10.0" 8 | 9 | scalacOptions ++= Seq("-deprecation", "-unchecked", "-feature") 10 | 11 | libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.1.0" 12 | 13 | initialCommands in console := """ 14 | import akka.actor._ 15 | import com.paulbutcher._ 16 | val system = ActorSystem("BuggyCache") 17 | """ -------------------------------------------------------------------------------- /03~并发网络 IO/IO 模型/Reactor/Proactor.md: -------------------------------------------------------------------------------- 1 | # Proactor 2 | 3 | Reactor 和 Proactor 模式的主要区别就是真正的读取和写入操作是有谁来完成的,Reactor 中需要应用程序自己读取或者写入数据,而 Proactor 模式中,应用程序不需要进行实际的读写过程,它只需要从缓存区读取或者写入即可,操作系统会读取缓存区或者写入缓存区到真正的 IO 设备。Proactor 模型的基本处理逻辑如下: 4 | 5 | - 应用程序初始化一个异步读取操作,然后注册相应的事件处理器,此时事件处理器不关注读取就绪事件,而是关注读取完成事件,这是区别于 Reactor 的关键。 6 | - 事件分离器等待读取操作完成事件。 7 | - 在事件分离器等待读取操作完成的时候,操作系统调用内核线程完成读取操作(异步 IO 都是操作系统负责将数据读写到应用传递进来的缓冲区供应用程序操作,操作系统扮演了重要角色),并将读取的内容放入用户传递过来的缓存区中。这也是区别于 Reactor 的一点,Proactor 中,应用程序需要传递缓存区。 8 | - 事件分离器捕获到读取完成事件后,激活应用程序注册的事件处理器,事件处理器直接从缓存区读取数据,而不需要进行实际的读取操作。 9 | -------------------------------------------------------------------------------- /03~并发网络 IO/README.md: -------------------------------------------------------------------------------- 1 | # 并发 IO 2 | 3 | IO 的概念,从字义来理解就是输入输出。操作系统从上层到底层,各个层次之间均存在 IO。比如,CPU 有 IO,内存有 IO, VMM 有 IO, 底层磁盘上也有 IO,这是广义上的 IO。通常来讲,一个上层的 IO 可能会产生针对磁盘的多个 IO,也就是说,上层的 IO 是稀疏的,下层的 IO 是密集的。 4 | 5 | 所谓的并发 IO,即在一个时间片内,如果一个进程进行一个 IO 操作,例如读个文件,这个时候该进程可以把自己标记为休眠状态并出让 CPU 的使用权,待文件读进内存,操作系统会把这个休眠的进程唤醒,唤醒后的进程就有机会重新获得 CPU 的使用权了。这里的进程在等待 IO 时之所以会释放 CPU 使用权,是为了让 CPU 在这段等待时间里可以做别的事情,这样一来 CPU 的使用率就上来了;此外,如果这时有另外一个进程也读文件,读文件的操作就会排队,磁盘驱动在完成一个进程的读操作后,发现有排队的任务,就会立即启动下一个读操作,这样 IO 的使用率也上来了。 6 | 7 | 典型的场景就是 Nginx 这样的反向代理服务器,Redis 这样的数据库以及各种 RPC 框架;它们的共通点就是会有大量的并发请求,并且每个请求都可能消耗一定的逻辑处理时间。 8 | -------------------------------------------------------------------------------- /02~并发模型/主从模型/README.md: -------------------------------------------------------------------------------- 1 | # 主从模型 2 | 3 | ```java 4 | public class WorkerThread extends Thread { 5 | 6 |     private final BlockingQueue queue; 7 |     public WorkerThread(BlockingQueue queue) { 8 |         this.queue = queue; 9 |     } 10 | 11 |     public void run() { 12 |         while (true) { 13 |             try { 14 |                 Runnable task = queue.take(); 15 |                 task.run(); 16 |             } catch (InterruptedException e) { 17 |                 break; /* 允许线程退出 */ 18 |             } 19 |         } 20 |     } 21 | } 22 | ``` 23 | -------------------------------------------------------------------------------- /04~异步编程/Futures & Promises/README.md: -------------------------------------------------------------------------------- 1 | # Futures & Promises 2 | 3 | Futures 和 Promises 是异步编程的一种流行抽象,尤其是在分布式系统的情况下。考虑一下简单的计算机处理器;没有并行性,只有一次完成一项任务或流程的能力。在这种情况下,有时在调用某些阻塞操作时会阻塞处理器。此类阻塞调用可能包括 I/O 操作,例如读/写磁盘,或通过网络发送或接收数据包。而且作为程序员,我们知道像 I/O 这样的阻塞调用要比典型的 CPU 绑定任务(如遍历列表)花费更多的时间。 4 | 5 | 处理器可以通过两种方式处理阻塞呼叫: 6 | 7 | - 同步:处理器等待,直到阻塞调用完成其任务并返回结果。之后,处理器将继续处理下一个任务。这有时可能会引起问题,因为可能无法有效利用 CPU。它可能会等待很长时间。 8 | 9 | - 异步:当异步处理任务时,在同步情况下等待所花费的 CPU 时间改为使用抢占式时间共享算法来处理其他任务。也就是说,不是等待,而是处理其他任务。因此,只要有更多工作可以完成,处理器就永远不会等待。 10 | 11 | 在编程领域,为了帮助程序员达到理想的资源利用率水平,引入了许多构造;Futures 与 Promises 即使其中最为广泛使用的概念之一。 12 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/WordCount/src/wordcount/words.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns wordcount.words) 10 | 11 | (defn get-words [text] 12 | (re-seq #"\w+" text)) 13 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Clojure/Transfer/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject fp-transfer "1.0" 10 | :dependencies [[org.clojure/clojure "1.4.0"]] 11 | :main transfer.core) 12 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/Sum/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject fp-sum "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"]] 11 | :main sum.core) 12 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/WordCount/src/wordcount/words.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns wordcount.words) 10 | 11 | (defn get-words [text] 12 | (re-seq #"\w+" text)) 13 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Clojure/Logger/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject transcript-handler "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"]] 11 | :main logger.core) 12 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/threading-essentials-course/src/main/java/com/islomar/CounterClient.java: -------------------------------------------------------------------------------- 1 | package com.islomar; 2 | 3 | // This client is thread-safe 4 | public class CounterClient { 5 | public static void main(String[] args) { 6 | Counter counter = new Counter(Integer.MAX_VALUE - 1); 7 | 8 | synchronized (counter) { 9 | if (counter.get() < Integer.MAX_VALUE) { 10 | System.out.println(counter.incrementAndGet()); 11 | } else { 12 | System.err.println("That would overflow"); 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /01~并发基础/01~内存模型/01~可见性与缓存一致性/README.md: -------------------------------------------------------------------------------- 1 | # 可见性与缓存一致性 2 | 3 | 所谓的可见性,即是一个线程对共享变量的修改,另外一个线程能够立刻看到。单核时代,所有的线程都是直接操作单个 CPU 的数据,某个线程对缓存的写对另外一个线程来说一定是可见的;譬如下图中,如果线程 B 在线程 A 更新了变量值之后进行访问,那么获得的肯定是变量 V 的最新值。多核时代,每颗 CPU 都有自己的缓存,共享变量存储在主内存。运行在某个 CPU 中的线程将共享变量读取到自己的 CPU 缓存。在 CPU 缓存中,修改了共享对象的值,由于 CPU 并未将缓存中的数据刷回主内存,导致对共享变量的修改对于在另一个 CPU 中运行的线程而言是不可见的。这样每个线程都会拥有一份属于自己的共享变量的拷贝,分别存于各自对应的 CPU 缓存中。 4 | 5 | ![CPU、内存与变量](https://ngte-superbed.oss-cn-beijing.aliyuncs.com/item/20230426174201.png) 6 | 7 | # Links 8 | 9 | - https://surfingcomplexity.blog/2022/11/25/cache-invalidation-really-is-one-of-the-hardest-things-in-computer-science/ 10 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/WordCountReducer/src/wordcount/words.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns wordcount.words) 10 | 11 | (defn get-words [text] 12 | (re-seq #"\w+" text)) 13 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/Reducers/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject fp-reducers "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"]] 11 | :main reducers.core) 12 | -------------------------------------------------------------------------------- /02~并发模型/生产者与消费者/Disruptor/API 使用/99~参考资料/2023~单机最快的队列 Disruptor 解析和使用.md: -------------------------------------------------------------------------------- 1 | > [原文地址](https://juejin.cn/post/7218389542587875384),文章详细介绍了 Disruptor 的原理、高性能秘诀、使用步骤和示例代码。 2 | > 3 | > Disruptor 是一个高性能的队列实现,由 LMAX 交易所开发,主要用于解决生产者、消费者及其数据存储的设计问题。它使用了一些关键技术来提高性能,如 CAS 操作、独占缓存行、环形队列、预分配内存等。 4 | > 5 | > 文章还提供了 Disruptor 的使用步骤,包括创建 Event、EventFactory、EventHandler、ExceptionHandler 类,实例化 Disruptor,设置消费者,创建 EventTranslator,发布事件等。 6 | > 7 | > 最后,文章给出了几个使用 Disruptor 的示例程序,包括单消费者、单消费者 Lambda 写法、多消费者重复消费元素、多消费者等场景的代码示例。 8 | > 9 | > Disruptor 是一个在 Java 领域广泛使用的高性能队列框架,适合需要高性能数据交换的场景。希望这篇文章能帮助您更好地理解和使用 Disruptor。 10 | 11 | # 单机最快的队列 Disruptor 解析和使用 12 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Clojure/DiningPhilosphersAtom/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject dining-philosphers-atom "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"]] 11 | :main philosophers.core) 12 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Clojure/DiningPhilosphersSTM/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject dining-philosphers-stm "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"]] 11 | :main philosophers.core) 12 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Clojure/DiningPhilosphersAtom2/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject dining-philosphers-atom2 "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"]] 11 | :main philosophers.core) 12 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/HttpDownload/src/main/java/com/paulbutcher/ProgressListener.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | interface ProgressListener { 12 | void onProgress(int current); 13 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Clojure/TranscriptTest/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject transcript-test "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"] 11 | [clj-http "0.7.0"]] 12 | :main test.core) 13 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/Zoom/src/main/resources/zoom.cl: -------------------------------------------------------------------------------- 1 | //--- 2 | // Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | // published by The Pragmatic Bookshelf. 4 | // Copyrights apply to this code. It may not be used to create training material, 5 | // courses, books, articles, and the like. Contact us if you are in doubt. 6 | // We make no guarantees that this code is fit for any purpose. 7 | // Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | //--- 9 | __kernel void zoom(__global float* vertices) { 10 | 11 | unsigned int id = get_global_id(0); 12 | vertices[id] *= 1.01; 13 | } 14 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/HttpDownloadFixed/src/main/java/com/paulbutcher/ProgressListener.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | interface ProgressListener { 12 | void onProgress(int current); 13 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/HttpDownloadBetter/src/main/java/com/paulbutcher/ProgressListener.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | interface ProgressListener { 12 | void onProgress(int current); 13 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/Sieve/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject csp-sieve "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"] 11 | [org.clojure/core.async "0.1.267.0-0d7780-alpha"]] 12 | :main sieve.core) 13 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/TranscriptTest/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject transcript-test "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"] 11 | [clj-http "0.7.0"]] 12 | :main test.core) 13 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Clojure/TranscriptHandler/src/server/charset.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns server.charset 10 | (:require [ring.util.response :refer [charset]])) 11 | 12 | (defn wrap-charset [handler] 13 | (fn [req] (charset (handler req) "UTF-8"))) -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Clojure/TranscriptTest/src/test/potato.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns test.potato) 10 | 11 | (def potato [ 12 | "One potato, two potato, three potato, four," 13 | "Five potato, six potato, seven potato, more." 14 | "" 15 | ""]) -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/SieveTimeout/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject csp-sievetimeout "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"] 11 | [org.clojure/core.async "0.1.267.0-0d7780-alpha"]] 12 | :main sieve.core) 13 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/README: -------------------------------------------------------------------------------- 1 | Most of these examples are written in C and compiled with GNU Make and GCC: 2 | 3 | http://www.gnu.org/software/make/ 4 | http://www.gnu.org/software/gcc/ 5 | 6 | To build, change into the directory of the project and: 7 | 8 | make 9 | ./target/ 10 | 11 | The Ripple and Zoom examples are written in Java and compiled with Maven: 12 | 13 | http://maven.apache.org/download.cgi 14 | 15 | To build and run, change into the directory of the project and: 16 | 17 | mvn compile 18 | env MAVEN_OPTS=-Djava.library.path=target/natives mvn exec:java 19 | 20 | Or use the run script: 21 | 22 | ./run 23 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/LambdaArchitecture/ExtractWikiText/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject extract-wiki-text "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"] 11 | [org.clojure/data.xml "0.0.7"]] 12 | :main wiki.core) 13 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountConcurrentHashMap/src/main/java/com/paulbutcher/PoisonPill.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | public class PoisonPill extends Page { 12 | public boolean isPoisonPill() { return true; } 13 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountProducerConsumer/src/main/java/com/paulbutcher/PoisonPill.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | public class PoisonPill extends Page { 12 | public boolean isPoisonPill() { return true; } 13 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/Polling/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject csp-polling "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"] 11 | [org.clojure/core.async "0.1.267.0-0d7780-alpha"]] 12 | :repl-options {:init-ns polling.core}) 13 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/TranscriptHandler2/src/server/charset.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns server.charset 10 | (:require [ring.util.response :refer [charset]])) 11 | 12 | (defn wrap-charset [handler] 13 | (fn [req] (charset (handler req) "UTF-8"))) -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountBatchConcurrentHashMap/src/main/java/com/paulbutcher/PoisonPill.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | public class PoisonPill extends Page { 12 | public boolean isPoisonPill() { return true; } 13 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountSynchronizedHashMap/src/main/java/com/paulbutcher/PoisonPill.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | public class PoisonPill extends Page { 12 | public boolean isPoisonPill() { return true; } 13 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/channels/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject csp-channels "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"] 11 | [org.clojure/core.async "0.1.267.0-0d7780-alpha"]] 12 | :repl-options {:init-ns channels.core}) 13 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Clojure/Logger/src/logger/core.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns logger.core) 10 | 11 | (defn now [] 12 | (System/currentTimeMillis)) 13 | 14 | (def log-entries (agent [])) 15 | 16 | (defn log [entry] 17 | (send log-entries conj [(now) entry])) 18 | -------------------------------------------------------------------------------- /01~并发基础/并发单元/进程/README.md: -------------------------------------------------------------------------------- 1 | # 进程与线程 2 | 3 | 在未配置 OS 的系统中,程序的执行方式是顺序执行,即必须在一个程序执行完后,才允许另一个程序执行;在多道程序环境下,则允许多个程序并发执行。程序的这两种执行方式间有着显著的不同。也正是程序并发执行时的这种特征,才导致了在操作系统中引入进程的概念。**进程是资源分配的基本单位,线程是资源调度的基本单位**。 4 | 5 | 进程是一种操作系统概念,用于描述具有单独的内存空间和资源的任务。在 Linux 中,使用 clone() 系统调用创建进程,该系统调用会克隆现有进程以创建新进程。clone() 调用接受各种标志,这些标志告诉 Linux 与原始进程共享/复制哪些资源。通常,不直接使用 clone() 系统调用。取而代之的是,我们使用在 glibc(用户空间)中实现的 POSIX 调用(如 fork())。实际上,我们使用的大多数 Linux 和 POSIX 接口都是在 glibc 中实现的,而不是在内核中实现的。 6 | 7 | 我们知道 fork() 调用通过使用带有一堆标志的 clone() 创建一个进程。使用 pthread_create() 调用创建线程。在后台,线程和进程都是任务,并由称为 task_struct 的结构表示。task_struct 大约有 170 个字段,大小约为 1k。一些值得注意的字段是:user,pid,tgid,files,fs,nsproxy 8 | 9 | - fs_struct `* fs` 包含有关当前根目录,工作目录,umask 等的信息。 10 | - pid 结构将进程映射到一个或多个任务 11 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/Translator/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject translator "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"] 11 | [compojure "1.1.5"] 12 | [ring/ring-jetty-adapter "1.1.8"]] 13 | :main server.core) 14 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/WordCount/src/wordcount/word_frequencies.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns wordcount.word-frequencies) 10 | 11 | (defn word-frequencies [words] 12 | (reduce 13 | (fn [counts word] (assoc counts word (inc (get counts word 0)))) 14 | {} words)) 15 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/DiningPhilosophers/src/main/java/com/paulbutcher/Chopstick.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | class Chopstick { 12 | private int id; 13 | public Chopstick(int id) { this.id = id; } 14 | public int getId() { return id; } 15 | } 16 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Actors/patterns/patterns.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | #--- 9 | defmodule Patterns do 10 | def foo({x, y}) do 11 | IO.puts("Got a pair, first element #{x}, second #{y}") 12 | end 13 | 14 | def foo({x, y, z}) do 15 | IO.puts("Got a triple: #{x}, #{y}, #{z}") 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/DiningPhilosophersFixed/src/main/java/com/paulbutcher/Chopstick.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | class Chopstick { 12 | private int id; 13 | public Chopstick(int id) { this.id = id; } 14 | public int getId() { return id; } 15 | } 16 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/DiningPhilosophersTimeout/src/main/java/com/paulbutcher/Chopstick.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | class Chopstick { 12 | private int id; 13 | public Chopstick(int id) { this.id = id; } 14 | public int getId() { return id; } 15 | } 16 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/TranscriptHandler/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject transcript-handler "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"] 11 | [compojure "1.1.5"] 12 | [ring/ring-jetty-adapter "1.1.8"]] 13 | :main server.core) 14 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCount/src/main/scala/com/paulbutcher/WordCount.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import akka.actor._ 12 | 13 | object WordCount extends App { 14 | 15 | val system = ActorSystem("WordCount") 16 | 17 | system.actorOf(Props[Master], "master") 18 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/WordCount/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject fp-wordcount "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"] 11 | [org.clojure/data.xml "0.0.7"]] 12 | :main wordcount.core 13 | :jvm-opts ^:replace ["-Xms4G" "-Xmx4G" "-XX:NewRatio=8" "-server"]) 14 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/Animation/resources/public/index.html: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | Animation 12 | 13 | 14 | 15 |
16 | 17 | 18 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Clojure/TranscriptHandler/src/server/translate.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns server.translate 10 | (:require [clj-http.client :as client])) 11 | 12 | (def translator "http://localhost:3001/translate") 13 | 14 | (defn translate [text] 15 | (future 16 | ((client/post translator {:body text}) :body))) 17 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCountMultipleCounters/src/main/scala/com/paulbutcher/WordCount.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import akka.actor._ 12 | 13 | object WordCount extends App { 14 | 15 | val system = ActorSystem("WordCount") 16 | 17 | system.actorOf(Props[Master], "master") 18 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/HelloClojureScript/resources/public/index.html: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | Hello ClojureScript 12 | 13 | 14 | 15 |
16 |
17 | 18 | 19 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Clojure/TournamentServer/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject tournament-server "1.0" 10 | :dependencies [[org.clojure/clojure "1.4.0"] 11 | [compojure "1.1.5"] 12 | [ring/ring-jetty-adapter "1.1.8"] 13 | [cheshire "5.0.1"]] 14 | :main server.core) 15 | -------------------------------------------------------------------------------- /04~异步编程/Futures & Promises/Promise 实践.md: -------------------------------------------------------------------------------- 1 | # Promise 实现 2 | 3 | # 隐式与显式 Promise 4 | 5 | # Promise Pipelining 6 | 7 | 传统 RPC 系统的缺陷之一是它们正在阻塞。设想一个场景,您需要调用一个 API“ A”和另一个 API“ B”,然后汇总这两个调用的结果并将该结果用作另一个 API“ C”的参数。现在,执行此操作的逻辑方法是并行调用 A 和 B,然后在完成后汇总结果并调用 C。不幸的是,在阻塞系统中,执行方法是调用 A,等待 完成它,调用 B,等待,然后聚合并调用 C。这似乎是在浪费时间,但是如果没有异步性,这是不可能的。即使具有异步性,线性管理或扩展系统也有些困难。幸运的是,我们有 Promise。 8 | 9 | ![Conventional RPC with Pipelining](http://dist-prog-book.com/chapter/2/images/p-2.png) 10 | 11 | Future/Promise 可以传递,等待或链接在一起。这些属性有助于使使用它们的程序员的生活更轻松。这也减少了与分布式计算相关的延迟。Promise 启用了数据流并发,这也是确定性的,并且更易于推理。 12 | 13 | # 异常处理 14 | 15 | # Futures and Promises in Action 16 | 17 | ## Twitter Finagle 18 | 19 | # Links 20 | 21 | - http://dist-prog-book.com/chapter/2/futures.html#implicit-vs-explicit-promises 22 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/LambdaArchitecture/ExtractWikiContributors/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject extract-wiki-contributors "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"] 11 | [org.clojure/data.xml "0.0.7"]] 12 | :main wiki.core 13 | :jvm-opts ^:replace ["-Xms4G" "-Xmx4G" "-XX:NewRatio=8" "-server"]) 14 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/TranscriptHandler2/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject transcript-handler2 "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"] 11 | [compojure "1.1.5"] 12 | [ring/ring-jetty-adapter "1.1.8"] 13 | [clj-http "0.7.0"]] 14 | :main server.core) 15 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/WordCountReducer/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject fp-wordcount "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"] 11 | [org.clojure/data.xml "0.0.7"] 12 | [foldable-seq "0.2"]] 13 | :main wordcount.core 14 | :jvm-opts ["-Xms4G" "-Xmx4G" "-XX:NewRatio=8"]) 15 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/MultiplyArrays/multiply_arrays.cl: -------------------------------------------------------------------------------- 1 | //--- 2 | // Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | // published by The Pragmatic Bookshelf. 4 | // Copyrights apply to this code. It may not be used to create training material, 5 | // courses, books, articles, and the like. Contact us if you are in doubt. 6 | // We make no guarantees that this code is fit for any purpose. 7 | // Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | //--- 9 | __kernel void multiply_arrays(__global const float* inputA, 10 | __global const float* inputB, 11 | __global float* output) { 12 | 13 | int i = get_global_id(0); 14 | output[i] = inputA[i] * inputB[i]; 15 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/Reducers/src/reducers/parallel_frequencies.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns reducers.parallel-frequencies 10 | (:require [clojure.core.reducers :as r])) 11 | 12 | (defn parallel-frequencies [coll] 13 | (r/fold 14 | (partial merge-with +) 15 | (fn [counts x] (assoc counts x (inc (get counts x 0)))) 16 | coll)) 17 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/MultiplyArraysProfiled/multiply_arrays.cl: -------------------------------------------------------------------------------- 1 | //--- 2 | // Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | // published by The Pragmatic Bookshelf. 4 | // Copyrights apply to this code. It may not be used to create training material, 5 | // courses, books, articles, and the like. Contact us if you are in doubt. 6 | // We make no guarantees that this code is fit for any purpose. 7 | // Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | //--- 9 | __kernel void multiply_arrays(__global const float* inputA, 10 | __global const float* inputB, 11 | __global float* output) { 12 | 13 | int i = get_global_id(0); 14 | output[i] = inputA[i] * inputB[i]; 15 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/threading-essentials-course/README.md: -------------------------------------------------------------------------------- 1 | # Threading essentials course 2 | Author: Heinz Kabutz 3 | 4 | ## Concurrency vs Parallelism 5 | * **Concurrency** 6 | * it's about dealing with lots of things at once. 7 | * example of the bus o waiting at the airport. 8 | * **Parallelism** 9 | * it's about doing lots of things at once. 10 | * you usually don't use threads. 11 | 12 | 13 | ## new Thread(), ExecutorService, ForkJoinPool, Parallel Streams 14 | * Parallel is not always faster, it depends on the input (example with already sorted array of integers). 15 | * Thread and ExecutorService: more about concurrency. 16 | * ForkJoinPool and Parallel Streams: more about parallelism. 17 | 18 | ## synchronized 19 | TBD 20 | 21 | ## VarHandles 22 | TBDRetina 13" Finales de 2013 -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Actors/word_count/mix.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | #--- 9 | defmodule WordCount.Mixfile do 10 | use Mix.Project 11 | 12 | def project do 13 | [ app: :word_count, 14 | version: "0.0.1", 15 | elixir: "~> 0.10.1", 16 | deps: deps ] 17 | end 18 | 19 | def application do 20 | [] 21 | end 22 | 23 | defp deps do 24 | [] 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/MultiplyArraysWithErrorHandling/multiply_arrays.cl: -------------------------------------------------------------------------------- 1 | //--- 2 | // Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | // published by The Pragmatic Bookshelf. 4 | // Copyrights apply to this code. It may not be used to create training material, 5 | // courses, books, articles, and the like. Contact us if you are in doubt. 6 | // We make no guarantees that this code is fit for any purpose. 7 | // Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | //--- 9 | __kernel void multiply_arrays(__global const float* inputA, 10 | __global const float* inputB, 11 | __global float* output) { 12 | 13 | int i = get_global_id(0); 14 | output[i] = inputA[i] * inputB[i]; 15 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountProducerConsumer/src/main/java/com/paulbutcher/Page.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | abstract class Page { 12 | public String getTitle() { throw new UnsupportedOperationException(); } 13 | public String getText() { throw new UnsupportedOperationException(); } 14 | public boolean isPoisonPill() { return false; } 15 | } 16 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountConcurrentHashMap/src/main/java/com/paulbutcher/Page.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | abstract class Page { 12 | public String getTitle() { throw new UnsupportedOperationException(); } 13 | public String getText() { throw new UnsupportedOperationException(); } 14 | public boolean isPoisonPill() { return false; } 15 | } 16 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountSynchronizedHashMap/src/main/java/com/paulbutcher/Page.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | abstract class Page { 12 | public String getTitle() { throw new UnsupportedOperationException(); } 13 | public String getText() { throw new UnsupportedOperationException(); } 14 | public boolean isPoisonPill() { return false; } 15 | } 16 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/WordCount/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject csp-wordcount "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"] 11 | [org.clojure/core.async "0.1.267.0-0d7780-alpha"] 12 | [rome/rome "1.0"] 13 | [http-kit "2.1.16"]] 14 | :main wordcount.core 15 | :jvm-opts ^:replace ["-Xms4G" "-Xmx4G" "-XX:NewRatio=8" "-server"]) 16 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountBatchConcurrentHashMap/src/main/java/com/paulbutcher/Page.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | abstract class Page { 12 | public String getTitle() { throw new UnsupportedOperationException(); } 13 | public String getText() { throw new UnsupportedOperationException(); } 14 | public boolean isPoisonPill() { return false; } 15 | } 16 | -------------------------------------------------------------------------------- /01~并发基础/并发单元/线程/线程模型/README.md: -------------------------------------------------------------------------------- 1 | # 线程模型 2 | 3 | # 链接 4 | 5 | - https://sctrack.sendcloud.net/track/click/eyJuZXRlYXNlIjogImZhbHNlIiwgIm1haWxsaXN0X2lkIjogMCwgInRhc2tfaWQiOiAiIiwgImVtYWlsX2lkIjogIjE2MjQwMTI4NTQzOTdfMTg3XzkwODA2XzQxOTQuc2MtMTBfOV8xM18yMTMtaW5ib3VuZDAkMzg0OTI0NTUyQHFxLmNvbSIsICJzaWduIjogIjg1NGU5Y2JkYWUyNjc1ZDYyMTNlNjNkZjRiNGQ2OGFhIiwgInVzZXJfaGVhZGVycyI6IHt9LCAibGFiZWwiOiAiNjE5MjAzMCIsICJ0cmFja19kb21haW4iOiAic2N0cmFjay5zZW5kY2xvdWQubmV0IiwgInJlYWxfdHlwZSI6ICIiLCAibGluayI6ICJodHRwcyUzQS8vdmlwLm1hbm9uZy5pby9ib3VuY2UlM0ZuaWQlM0Q1NSUyNmFpZCUzRDIyNTIlMjZ1cmwlM0RodHRwcyUyNTNBJTI1MkYlMjUyRnRvdXRpYW8uaW8lMjUyRmslMjUyRmdmbzNpYmElMjZuJTNETVRNeS43MGt5ckJjZ0l1OHNJZHlnWDE4RVhRZFJFYUkiLCAib3V0X2lwIjogIjEyMC4xMzIuNTQuMTk0IiwgImNvbnRlbnRfdHlwZSI6IDMsICJ1c2VyX2lkIjogMTg3LCAib3ZlcnNlYXMiOiAiZmFsc2UiLCAiY2F0ZWdvcnlfaWQiOiA2MDM0OX0=.html 一文理解JVM线程属于用户态还是内核态 6 | 7 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCount/src/main/java/com/paulbutcher/Page.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | class Page { 12 | private String title; 13 | private String text; 14 | 15 | public Page(String title, String text) { this.title = title; this.text = text; } 16 | 17 | public String getTitle() { return title; } 18 | public String getText() { return text; } 19 | } 20 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Actors/counter/counter.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | #--- 9 | defmodule Counter do 10 | def start(count) do 11 | spawn(__MODULE__, :loop, [count]) 12 | end 13 | 14 | def next(counter) do 15 | send(counter, {:next}) 16 | end 17 | 18 | def loop(count) do 19 | receive do 20 | {:next} -> 21 | IO.puts("Current count: #{count}") 22 | loop(count + 1) 23 | end 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Clojure/TranscriptHandler/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject transcript-handler "1.0" 10 | :dependencies [[org.clojure/clojure "1.5.1"] 11 | [compojure "1.1.5"] 12 | [ring/ring-jetty-adapter "1.1.8"] 13 | [clj-http "0.7.0"] 14 | [schejulure "0.1.4"] 15 | [org.flatland/useful "0.10.0"]] 16 | :main server.core) 17 | -------------------------------------------------------------------------------- /01~并发基础/并发单元/线程/线程池/README.md: -------------------------------------------------------------------------------- 1 | # 线程池 2 | 3 | 线程池是一种使用户可以访问可以进行工作的一组就绪的空闲线程的抽象。线程池的实现会照顾到工作线程的创建,管理和调度,如果不小心处理,它们很容易变得棘手且代价高昂。线程池具有许多不同的风格,具有许多用于调度和执行任务的技术,并且具有固定数量的线程,或者具有根据负载动态调整自身大小的能力。Java 的 Executor 就是典型的线程池的实现,Executor 包含了一系列 Runnable 的任务,它对上屏蔽了任务具体执行细节的抽象。这些详细信息(例如选择运行任务的线程,任务的计划方式)由 Executor 接口的基础实现管理。 4 | 5 | 类似于 Executor,Scala 在 `scala.concurrent` 包中提供了 ExecutionContext 类,其基础的特性类似于 Java 的 Executor;它负责并发高效地执行计算,而无需池中的用户去担心诸如调度之类的事情。更重要地是,ExecutionContext 同样可被当做接口,我们能够改变线程池地底层实现而保证上层接口的一致性。在诸多的实现中,Scala 的默认 ExecutionContext 的实现是基于 Java 的 ForkJoinPool,一种具有 work-stealing 算法的线程池实现,其中空闲线程拾取先前安排给其他繁忙线程的任务。ForkJoinPool 是一种流行的线程池实现,因为它比 Executors 的性能有所提高,能够更好地避免池引起的死锁,并最大程度地减少了在线程之间切换所花费的时间。 6 | 7 | # Links 8 | 9 | - https://mp.weixin.qq.com/s/skBA9RwVBLnw8BYZhcUSrA Java 线程池原理及最佳实践(1.5W 字,面试必问) 10 | 11 | - https://mp.weixin.qq.com/s/gTiffvjwUulT2nDbt68j6Q 线程池优化与监控 -------------------------------------------------------------------------------- /01~并发基础/01~内存模型/03~原子性/03~原子性.md: -------------------------------------------------------------------------------- 1 | # 原子性 2 | 3 | 所谓的原子性,就是一个或者多个操作在 CPU 执行的过程中不被中断的特性,CPU 能保证的原子操作是 CPU 指令级别的,而不是高级语言的操作符。我们在编程语言中部分看似原子操作的指令,在被编译到汇编之后往往会变成多个操作: 4 | 5 | ```s 6 | i++ 7 | 8 | # 编译成汇编之后就是: 9 | # 读取当前变量 i 并把它赋值给一个临时寄存器; 10 | movl i(%rip), %eax 11 | # 给临时寄存器+1; 12 | addl $1, %eax 13 | # 把 eax 的新值写回内存 14 | movl %eax, i(%rip) 15 | ``` 16 | 17 | 我们可以清楚看到 C 代码只需要一句,但编译成汇编却需要三步(这里不考虑编译器优化,实际上通过编译器优化可以将这三条汇编指令合并成一条)。也就是说,只有简单的读取、赋值(而且必须是将数字赋值给某个变量,变量之间的相互赋值不是原子操作)才是原子操作。按照原子操作解决同步问题方式:依靠处理器原语支持把上述三条指令合三为一,当做一条指令来执行,保证在执行过程中不会被打断并且多线程并发也不会受到干扰。这样同步问题迎刃而解,这也就是所谓的原子操作。但处理器没有义务为任意代码片段提供原子性操作,尤其是我们的临界区资源十分庞大甚至大小不确定,处理器没有必要或是很难提供原子性支持,此时往往需要依赖于锁来保证原子性。 18 | 19 | 对应原子操作/事务在 Java 中,对基本数据类型的变量的读取和赋值操作是原子性操作,即这些操作是不可被中断的,要么执行,要么不执行。Java 内存模型只保证了基本读取和赋值是原子性操作,如果要实现更大范围操作的原子性,可以通过 synchronized 和 Lock 来实现。由于 synchronized 和 Lock 能够保证任一时刻只有一个线程执行该代码块,那么自然就不存在原子性问题了,从而保证了原子性。 20 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Actors/links/links.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | #--- 9 | defmodule LinkTest do 10 | def loop do 11 | receive do 12 | {:exit_because, reason} -> exit(reason) 13 | {:link_to, pid} -> Process.link(pid) 14 | {:EXIT, pid, reason} -> IO.puts("#{inspect(pid)} exited because #{reason}") 15 | end 16 | loop 17 | end 18 | 19 | def loop_system do 20 | Process.flag(:trap_exit, true) 21 | loop 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountConcurrentHashMap/src/main/java/com/paulbutcher/WikiPage.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | class WikiPage extends Page { 12 | private String title; 13 | private String text; 14 | 15 | public WikiPage(String title, String text) { this.title = title; this.text = text; } 16 | 17 | public String getTitle() { return title; } 18 | public String getText() { return text; } 19 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountProducerConsumer/src/main/java/com/paulbutcher/WikiPage.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | class WikiPage extends Page { 12 | private String title; 13 | private String text; 14 | 15 | public WikiPage(String title, String text) { this.title = title; this.text = text; } 16 | 17 | public String getTitle() { return title; } 18 | public String getText() { return text; } 19 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountBatchConcurrentHashMap/src/main/java/com/paulbutcher/WikiPage.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | class WikiPage extends Page { 12 | private String title; 13 | private String text; 14 | 15 | public WikiPage(String title, String text) { this.title = title; this.text = text; } 16 | 17 | public String getTitle() { return title; } 18 | public String getText() { return text; } 19 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountSynchronizedHashMap/src/main/java/com/paulbutcher/WikiPage.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | class WikiPage extends Page { 12 | private String title; 13 | private String text; 14 | 15 | public WikiPage(String title, String text) { this.title = title; this.text = text; } 16 | 17 | public String getTitle() { return title; } 18 | public String getText() { return text; } 19 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/threading-essentials-course/src/main/java/com/islomar/ExecutorServiceExample.java: -------------------------------------------------------------------------------- 1 | package com.islomar; 2 | 3 | import java.time.LocalTime; 4 | import java.util.concurrent.Executors; 5 | import java.util.concurrent.ScheduledExecutorService; 6 | import java.util.concurrent.TimeUnit; 7 | 8 | public class ExecutorServiceExample { 9 | 10 | public static void main(String... args) { 11 | ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor(); 12 | timer.scheduleAtFixedRate( 13 | () -> System.out.println(LocalTime.now()), 14 | 0, 1, TimeUnit.SECONDS); 15 | 16 | timer.scheduleAtFixedRate( 17 | () -> System.out.println("Hello world"), 18 | 0, 200, TimeUnit.MILLISECONDS); 19 | 20 | System.out.println("Main: I'm done, outta here!!"); 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Actors/parallel/parallel.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | #--- 9 | defmodule Parallel do 10 | def map(collection, fun) do 11 | parent = self() 12 | 13 | processes = Enum.map(collection, fn(e) -> 14 | spawn_link(fn() -> 15 | send(parent, {self(), fun.(e)}) 16 | end) 17 | end) 18 | 19 | Enum.map(processes, fn(pid) -> 20 | receive do 21 | {^pid, result} -> result 22 | end 23 | end) 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/threading-essentials-course/src/main/java/com/islomar/Counter.java: -------------------------------------------------------------------------------- 1 | package com.islomar; 2 | 3 | // The counter is thread-safe 4 | public class Counter { 5 | 6 | // Invariant: value >= 0 7 | private int value; 8 | 9 | public Counter(int value) { 10 | if (value < 0) 11 | throw new ArithmeticException("overflow"); 12 | this.value = value; 13 | } 14 | 15 | public synchronized int incrementAndGet() { 16 | if (value == Integer.MAX_VALUE) 17 | throw new ArithmeticException("overflow"); 18 | return ++value; 19 | } 20 | 21 | public synchronized int getAndIncrement() { 22 | if (value == Integer.MAX_VALUE) 23 | throw new ArithmeticException("overflow"); 24 | return value++; 25 | } 26 | 27 | public synchronized int get() { 28 | return value; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCount/src/main/scala/com/paulbutcher/Counter.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import akka.actor._ 12 | import collection.mutable.HashMap 13 | 14 | class Counter extends Actor { 15 | 16 | val counts = HashMap[String, Int]().withDefaultValue(0) 17 | 18 | def receive = { 19 | case Page(title, text) => 20 | for (word <- Words(text)) 21 | counts(word) += 1 22 | sender ! Processed 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/TranscriptTest/src/test/core.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns test.core 10 | (:require [test.jabberwocky :refer :all] 11 | [clj-http.client :as client])) 12 | 13 | (future 14 | (doseq [n (iterate inc 0)] 15 | (println (:body (client/get (str "http://localhost:3000/translation/" n)))))) 16 | 17 | (defn -main [& args] 18 | (doseq [n (range (count jabberwocky))] 19 | (client/put (str "http://localhost:3000/snippet/" n) {:body (nth jabberwocky n)}))) -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/DateFormatBug/src/main/java/com/paulbutcher/DateParser.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | import java.text.DateFormat; 12 | import java.text.ParseException; 13 | import java.text.SimpleDateFormat; 14 | import java.util.Date; 15 | 16 | class DateParser { 17 | private final DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 18 | 19 | public Date parse(String s) throws ParseException { 20 | return format.parse(s); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /02~并发模型/生产者与消费者/Disruptor/99~参考资料/2017~Disruptor 无锁缓存框架.md: -------------------------------------------------------------------------------- 1 | > [原文地址](https://blog.csdn.net/f59130/article/details/74014048),您提供的链接是一篇关于 Java 并发编程中的 Disruptor 无锁缓存框架的 CSDN 博客文章。文章详细介绍了 Disruptor 框架的基本概念、实现原理、使用方式以及它如何解决 CPU Cache 伪共享问题。 2 | > 3 | > 以下是文章的主要内容概述: 4 | > 5 | > 1. **生产者消费者模型**:介绍了传统的生产者消费者模型,通常使用`BlockingQueue`实现,并通过代码示例展示了如何实现这一模型。 6 | > 2. **BlockingQueue 的不足**:指出了`ArrayBlockingQueue`在高并发场景下的性能问题,因为它使用锁和阻塞等待实现线程同步。 7 | > 3. **Disruptor 初体验**:介绍了 Disruptor 框架,由 LMAX 公司开发,是一个高效无锁内存队列,使用环形队列代替线性队列,并通过 CAS 操作实现线程安> 全。 8 | > 4. **Disruptor 小试牛刀**:通过一个具体的示例,展示了如何在项目中使用 Disruptor 框架,包括添加 Maven 依赖、定义事件对象、工厂类、消费者、生> 产者以及如何整合这些组件。 9 | > 5. **Disruptor 策略**:介绍了 Disruptor 提供的几种等待策略,包括`BlockingWaitStrategy`、`SleepingWaitStrategy`、> `YieldingWaitStrategy`和`BusySpinWaitStrategy`,每种策略适用于不同的场景。 10 | > 6. **解决 CPU Cache 伪共享问题**:解释了 CPU Cache 伪共享问题的原因和影响,并介绍了 Disruptor 如何解决这一问题,通过代码示例展示了如何使用> 填充(padding)来避免伪共享。 11 | > 12 | > 文章最后提供了一个参考链接,供读者进一步了解 Disruptor 的详细信息。 13 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/LambdaArchitecture/ExtractWikiText/src/wiki/core.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns wiki.core 10 | (:require [wiki.pages :refer :all])) 11 | 12 | (defn -main [& args] 13 | (try 14 | (let [[infile outfile page-count] args 15 | pages (get-pages infile) 16 | trimmed-pages (if page-count (take (Integer. page-count) pages) pages)] 17 | (doseq [page trimmed-pages] 18 | (spit outfile page :append true))) 19 | (catch Exception _ (println "Usage: ")))) 20 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/HelloWorld/src/main/java/com/paulbutcher/HelloWorld.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | public class HelloWorld { 12 | 13 | public static void main(String[] args) throws InterruptedException { 14 | Thread myThread = new Thread() { 15 | public void run() { 16 | System.out.println("Hello from new thread"); 17 | } 18 | }; 19 | myThread.start(); 20 | System.out.println("Hello from main thread"); 21 | myThread.join(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Actors/counter/counter2.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | #--- 9 | defmodule Counter do 10 | def start(count) do 11 | spawn(__MODULE__, :loop, [count]) 12 | end 13 | 14 | def next(counter) do 15 | ref = make_ref() 16 | send(counter, {:next, self(), ref}) 17 | receive do 18 | {:ok, ^ref, count} -> count 19 | end 20 | end 21 | 22 | def loop(count) do 23 | receive do 24 | {:next, sender, ref} -> 25 | send(sender, {:ok, ref, count}) 26 | loop(count + 1) 27 | end 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/Sum/src/sum/core.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns sum.core 10 | (:require [clojure.core.reducers :as r])) 11 | 12 | (defn recursive-sum [numbers] 13 | (if (empty? numbers) 14 | 0 15 | (+ (first numbers) (recursive-sum (rest numbers))))) 16 | 17 | (defn reduce-sum [numbers] 18 | (reduce (fn [x y] (+ x y)) 0 numbers)) 19 | 20 | (defn sum [numbers] 21 | (reduce + numbers)) 22 | 23 | (defn apply-sum [numbers] 24 | (apply + numbers)) 25 | 26 | (defn parallel-sum [numbers] 27 | (r/fold + numbers)) 28 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/Paths/src/main/scala/com/paulbutcher/TestActor.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import akka.actor._ 12 | 13 | case class CreateChild(name: String) 14 | case object SayHello 15 | case class SayHelloFrom(path: String) 16 | 17 | class TestActor extends Actor { 18 | 19 | def receive = { 20 | case CreateChild(name) => context.actorOf(Props[TestActor], name) 21 | case SayHello => println(s"Hello from $self") 22 | case SayHelloFrom(path) => context.actorFor(path) ! SayHello 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCount/src/main/scala/com/paulbutcher/Parser.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import akka.actor._ 12 | 13 | case object Processed 14 | 15 | class Parser(counter: ActorRef) extends Actor { 16 | 17 | val pages = Pages(100000, "enwiki.xml") 18 | 19 | override def preStart { 20 | for (page <- pages.take(10)) 21 | counter ! page 22 | } 23 | 24 | def receive = { 25 | case Processed if pages.hasNext => counter ! pages.next 26 | case _ => context.stop(self) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/Polling/src/polling/core.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns polling.core 10 | (:require [clojure.core.async :as async :refer :all :exclude [map into reduce merge partition partition-by take]])) 11 | 12 | (defn poll-fn [interval action] 13 | (let [seconds (* interval 1000)] 14 | (go (while true 15 | (action) 16 | (sentences [strings] 22 | (filter is-sentence? 23 | (reductions sentence-join 24 | (mapcat sentence-split strings)))) 25 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/WordCount/src/wordcount/polling.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns wordcount.polling 10 | (:require [clojure.core.async :as async :refer :all :exclude [map into reduce merge partition partition-by take]])) 11 | 12 | (defn poll-fn [interval action] 13 | (let [seconds (* interval 1000)] 14 | (go (while true 15 | (action) 16 | ( counters ! pages.next 26 | case _ => context.stop(self) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /03~并发网络 IO/IO 模型/Reactor/README.md: -------------------------------------------------------------------------------- 1 | # Reactor 2 | 3 | 在[《Linux 线程模型》](https://ng-tech.icu/books/Linux-Notes/#/)中我们讨论过 Linux 中线程的实现以及用户态线程与内核态线程中等概念,在这里我们讨论的线程模型,则着眼于如何高效的利用多个物理核,进行工作任务的调度,使得系统能够有更高有效的吞吐,更加低的延迟。而不是被某个等待 IO 的线程阻塞或者把时间花在大量的比如系统层面的上下文切换等工作。 4 | 5 | 网络 IO 中最简单的就是连接独占模型:也就是一个连接进来请求后,独占一个线程(进程)进行处理;无论其中中间在做什么事情,比如调用第三方的服务,等待过程中也是独占着整个线程。或者依赖多开线程的方式来提供服务端的吞吐。但是多开线程势必带来的问题就是系统层面的开销比较大(contex-switch、cache-bounding 等等),对于高性能场景典型就不太适用。传统的如 Tomcat Servlet 就是基于这样的原理,而我们现在实际应用中常用的是 Reactor 模型。Doug Lea 把 Reactor 模式分为三种类型,分别是:Basic version, Multi-threaded version 和 Multi-reactors version。 6 | 7 | > 实践案例可以参阅《[Database-Notes/Redis](https://github.com/wx-chevalier/Database-Notes?q=)》等相关章节。 8 | 9 | 单线程 Reactor 模型譬如使用单个线程处理所有连接上的请求,使用 epoll-wait 方式,实现事件多路复用机制;典型比如 Redis,适用于简单比如小数据的内存数据的获取,每一个回调逻辑都比较简单。还有多线程 Reactor 模型,即多个线程/进程 Accept 同一个连接上的请求。Reactor 模型在 Linux 系统中的具体实现即是 select/poll/epoll/kqueue,像 Redis 中即是采用了 Reactor 模型实现了单进程单线程高并发。 10 | 11 | # Links 12 | 13 | - https://cubox.pro/c/mMiamX 彻底搞懂事件驱动模型 - Reactor 14 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/TranscriptHandler2/src/server/sentences.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns server.sentences 10 | (:require [clojure.string :refer [trim]])) 11 | 12 | (defn sentence-split [text] 13 | (map trim (re-seq #"[^\.!\?:;]+[\.!\?:;]*" text))) 14 | 15 | (defn is-sentence? [text] 16 | (re-matches #"^.*[\.!\?:;]$" text)) 17 | 18 | (defn sentence-join [x y] 19 | (if (is-sentence? x) y (str x " " y))) 20 | 21 | (defn strings->sentences [strings] 22 | (filter is-sentence? 23 | (reductions sentence-join 24 | (mapcat sentence-split strings)))) 25 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/Animation/src-clj/animation/core.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns animation.core 10 | (:require [compojure.core :refer :all] 11 | [compojure.handler :refer [site]] 12 | [compojure.route :refer [resources not-found]] 13 | [ring.util.response :refer [redirect]] 14 | [ring.adapter.jetty :refer [run-jetty]])) 15 | 16 | (defroutes app-routes 17 | (GET "/" [] (redirect "/index.html")) 18 | (resources "/") 19 | (not-found "Page not found")) 20 | 21 | (defn -main [& args] 22 | (run-jetty (site app-routes) {:port 3000})) 23 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Actors/counter/counter3.ex: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | #--- 9 | defmodule Counter do 10 | def loop(count) do 11 | receive do 12 | {:next, sender, ref} -> 13 | send(sender, {:ok, ref, count}) 14 | loop(count + 1) 15 | end 16 | end 17 | 18 | def start(count) do 19 | pid = spawn(__MODULE__, :loop, [count]) 20 | Process.register(pid, :counter) 21 | pid 22 | end 23 | 24 | def next do 25 | ref = make_ref() 26 | send(:counter, {:next, self(), ref}) 27 | receive do 28 | {:ok, ^ref, count} -> count 29 | end 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/LambdaArchitecture/ExtractWikiContributors/src/wiki/core.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns wiki.core 10 | (:require [wiki.contributors :refer :all] 11 | [clojure.java.io :as io] 12 | [clojure.string :as string])) 13 | 14 | (defn -main [& args] 15 | (try 16 | (let [[infile outfile] args] 17 | (with-open [out (io/writer outfile)] 18 | (doseq [contribution (get-contributions infile)] 19 | (.write out (string/join " " contribution)) 20 | (.write out "\n")))) 21 | (catch Exception _ (println "Usage: ")))) 22 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Actors/hello_actors/hello_actors.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | #--- 9 | import :timer, only: [ sleep: 1 ] 10 | 11 | defmodule Talker do 12 | def loop do 13 | receive do 14 | {:greet, name} -> IO.puts("Hello #{name}") 15 | {:praise, name} -> IO.puts("#{name}, you're amazing") 16 | {:celebrate, name, age} -> IO.puts("Here's to another #{age} years, #{name}") 17 | end 18 | loop 19 | end 20 | end 21 | 22 | pid = spawn(&Talker.loop/0) 23 | 24 | send(pid, {:greet, "Huey"}) 25 | send(pid, {:praise, "Dewey"}) 26 | send(pid, {:celebrate, "Louie", 16}) 27 | 28 | sleep(1000) 29 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCountMultipleCounters/src/main/scala/com/paulbutcher/Counter.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import akka.actor._ 12 | import collection.mutable.HashMap 13 | 14 | class Counter(accumulator: ActorRef) extends Actor { 15 | 16 | val counts = HashMap[String, Int]().withDefaultValue(0) 17 | 18 | def receive = { 19 | case Page(title, text) => 20 | for (word <- Words(text)) 21 | counts(word) += 1 22 | sender ! Processed 23 | } 24 | 25 | override def postStop() { 26 | accumulator ! Counts(counts) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCount/src/main/scala/com/paulbutcher/Master.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import akka.actor._ 12 | 13 | class Master extends Actor { 14 | 15 | val counter = context.actorOf(Props[Counter], "counter") 16 | val parser = context.actorOf(Props(new Parser(counter)), "parser") 17 | 18 | override def preStart { 19 | context.watch(counter) 20 | context.watch(parser) 21 | } 22 | 23 | def receive = { 24 | case Terminated(`parser`) => counter ! PoisonPill 25 | case Terminated(`counter`) => context.system.shutdown 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/HelloClojureScript/src-clj/hello_clojurescript/core.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns hello-clojurescript.core 10 | (:require [compojure.core :refer :all] 11 | [compojure.handler :refer [site]] 12 | [compojure.route :refer [resources not-found]] 13 | [ring.util.response :refer [redirect]] 14 | [ring.adapter.jetty :refer [run-jetty]])) 15 | 16 | (defroutes app-routes 17 | (GET "/" [] (redirect "/index.html")) 18 | (resources "/") 19 | (not-found "Page not found")) 20 | 21 | (defn -main [& args] 22 | (run-jetty (site app-routes) {:port 3000})) 23 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountConcurrentHashMap/src/main/java/com/paulbutcher/Parser.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | import java.util.concurrent.BlockingQueue; 12 | 13 | class Parser implements Runnable { 14 | 15 | private BlockingQueue queue; 16 | 17 | public Parser(BlockingQueue queue) { 18 | this.queue = queue; 19 | } 20 | 21 | public void run() { 22 | try { 23 | Iterable pages = new Pages(100000, "enwiki.xml"); 24 | for (Page page: pages) 25 | queue.put(page); 26 | } catch (Exception e) { e.printStackTrace(); } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountProducerConsumer/src/main/java/com/paulbutcher/Parser.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | import java.util.concurrent.BlockingQueue; 12 | 13 | class Parser implements Runnable { 14 | 15 | private BlockingQueue queue; 16 | 17 | public Parser(BlockingQueue queue) { 18 | this.queue = queue; 19 | } 20 | 21 | public void run() { 22 | try { 23 | Iterable pages = new Pages(100000, "enwiki.xml"); 24 | for (Page page: pages) 25 | queue.put(page); 26 | } catch (Exception e) { e.printStackTrace(); } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountBatchConcurrentHashMap/src/main/java/com/paulbutcher/Parser.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | import java.util.concurrent.BlockingQueue; 12 | 13 | class Parser implements Runnable { 14 | 15 | private BlockingQueue queue; 16 | 17 | public Parser(BlockingQueue queue) { 18 | this.queue = queue; 19 | } 20 | 21 | public void run() { 22 | try { 23 | Iterable pages = new Pages(100000, "enwiki.xml"); 24 | for (Page page: pages) 25 | queue.put(page); 26 | } catch (Exception e) { e.printStackTrace(); } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountSynchronizedHashMap/src/main/java/com/paulbutcher/Parser.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | import java.util.concurrent.BlockingQueue; 12 | 13 | class Parser implements Runnable { 14 | 15 | private BlockingQueue queue; 16 | 17 | public Parser(BlockingQueue queue) { 18 | this.queue = queue; 19 | } 20 | 21 | public void run() { 22 | try { 23 | Iterable pages = new Pages(100000, "enwiki.xml"); 24 | for (Page page: pages) 25 | queue.put(page); 26 | } catch (Exception e) { e.printStackTrace(); } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/FindMinimumOneWorkGroup/find_minimum.cl: -------------------------------------------------------------------------------- 1 | //--- 2 | // Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | // published by The Pragmatic Bookshelf. 4 | // Copyrights apply to this code. It may not be used to create training material, 5 | // courses, books, articles, and the like. Contact us if you are in doubt. 6 | // We make no guarantees that this code is fit for any purpose. 7 | // Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | //--- 9 | __kernel void find_minimum(__global const float* values, 10 | __global float* result, 11 | __local float* scratch) { 12 | 13 | int i = get_global_id(0); 14 | int n = get_global_size(0); 15 | 16 | scratch[i] = values[i]; 17 | 18 | barrier(CLK_LOCAL_MEM_FENCE); 19 | 20 | for (int j = n / 2; j > 0; j /= 2) { 21 | if (i < j) 22 | scratch[i] = min(scratch[i], scratch[i + j]); 23 | barrier(CLK_LOCAL_MEM_FENCE); 24 | } 25 | 26 | if (i == 0) 27 | *result = scratch[0]; 28 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/Wizard/src-clj/wizard/core.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns wizard.core 10 | (:require [compojure.core :refer :all] 11 | [compojure.handler :refer [site]] 12 | [compojure.route :refer [resources not-found]] 13 | [ring.util.response :refer [redirect]] 14 | [ring.adapter.jetty :refer [run-jetty]])) 15 | 16 | (defroutes app-routes 17 | (GET "/" [] (redirect "/index.html")) 18 | (POST "/wizard" {params :params} (str "" params "")) 19 | (resources "/") 20 | (not-found "Page not found")) 21 | 22 | (defn -main [& args] 23 | (run-jetty (site app-routes) {:port 3000})) 24 | -------------------------------------------------------------------------------- /04~异步编程/反应式编程/反压/反压.md: -------------------------------------------------------------------------------- 1 | # Backpressure(反压) 2 | 3 | 首先,Backpressure 并不是响应式编程(Reactive Programming,或者有的人喜欢按字直译为反应式编程)独有的;其次,Backpressure 并不是一种机制,也不是一种策略。Backpressure 其实是一种现象:在数据流从上游生产者向下游消费者传输的过程中,上游生产速度大于下游消费速度,导致下游的 Buffer 溢出,这种现象就叫做 Backpressure 出现。 4 | 5 | 编程中的 Backpressure 这个概念源自工程概念中的 Backpressure:在管道运输中,气流或液流由于管道突然变细、急弯等原因导致由某处出现了下游向上游的逆向压力,这种情况称作 back pressure。Backpressure 和 Buffer 是一对相生共存的概念,只有设置了 Buffer,才有 Backpressure 出现;只要设置了 Buffer,一定存在出现 Backpressure 的风险。 6 | 7 | 例如你是开发服务器后端的,有一个 Socket 不断地接收来自用户的 http 请求来把用户需要的网页返回给用户。你的服务器所能承受的同时访问用户数是有上限的吧?比如说,你的服务器主机的处理器和内存情况决定了,它最多只能承受 5000~6000 个用户同时访问,再多的话服务器就有当掉的风险了。那么你决定:把用户数上限设置为 5000,当超出 5000 用户数的时候,再有新的访问就把它丢弃或者拒绝。那么对于这个案例,5000 就是你对于用户访问数设置的 Buffer;第 5001 个用户的访问,就叫做造成了 Backpressure 的产生;而你的丢弃或拒绝的行为,就是对于 Backpressure 的处理。 8 | 9 | 生产速度大于消费速度,所以需要 Buffer;外部条件有限制,所以 Buffer 需要有上限;Buffer 达到上限这个现象,有一个简化的等价词叫做 Backpressure;Backpressure 的出现其实是一种危险边界,唯一的选择是丢弃新事件。总而言之,Backpressure 指的是在 Buffer 有上限的系统中,Buffer 溢出的现象;它的应对措施只有一个:丢弃新事件。Backpressure 只是一种现象,而不是一种机制;至于你说的 throttleFirst、debounce,更不是某个机制中的一环,它们只是可以通过人为过滤的方式来降低生产速度,从而降低 Backpressure 出现的几率罢了。 10 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCount/src/main/scala/com/paulbutcher/Words.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import java.text.BreakIterator 12 | 13 | class Words(text: String) extends Iterator[String] { 14 | val wordBoundary = BreakIterator.getWordInstance 15 | wordBoundary.setText(text) 16 | var start = wordBoundary.first 17 | var end = wordBoundary.next 18 | 19 | def hasNext = end != BreakIterator.DONE 20 | 21 | def next() = { 22 | val s = text.subSequence(start, end) 23 | start = end 24 | end = wordBoundary.next 25 | s.toString 26 | } 27 | } 28 | 29 | object Words { 30 | def apply(text:String) = new Words(text) 31 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/FindMinimumMultipleWorkGroups/find_minimum.cl: -------------------------------------------------------------------------------- 1 | //--- 2 | // Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | // published by The Pragmatic Bookshelf. 4 | // Copyrights apply to this code. It may not be used to create training material, 5 | // courses, books, articles, and the like. Contact us if you are in doubt. 6 | // We make no guarantees that this code is fit for any purpose. 7 | // Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | //--- 9 | __kernel void find_minimum(__global const float* values, 10 | __global float* results, 11 | __local float* scratch) { 12 | 13 | int i = get_local_id(0); 14 | int n = get_local_size(0); 15 | 16 | scratch[i] = values[get_global_id(0)]; 17 | 18 | barrier(CLK_LOCAL_MEM_FENCE); 19 | 20 | for (int j = n / 2; j > 0; j /= 2) { 21 | if (i < j) 22 | scratch[i] = min(scratch[i], scratch[i + j]); 23 | barrier(CLK_LOCAL_MEM_FENCE); 24 | } 25 | 26 | if (i == 0) 27 | results[get_group_id(0)] = scratch[0]; 28 | } -------------------------------------------------------------------------------- /00~并发导论/01~并发的特性/并发级别.md: -------------------------------------------------------------------------------- 1 | # 并发维度 2 | 3 | ## 线程级并发 4 | 5 | 从 20 世纪 60 年代初期出现时间共享以来,计算机系统中就开始有了对并发执行的支持;传统意义上,这种并发执行只是模拟出来的,是通过使一台计算机在它正在执行的进程间快速切换的方式实现的,这种配置称为单处理器系统。从 20 世纪 80 年代开始,多处理器系统,即由单操作系统内核控制的多处理器组成的系统采用了多核处理器与超线程(HyperThreading)等技术允许我们实现真正的并行。多核处理器是将多个 CPU 集成到一个集成电路芯片上: 6 | 7 | ![image](https://user-images.githubusercontent.com/5803001/52341286-21d58300-2a4d-11e9-85fe-5fe5f3894d66.png) 8 | 9 | 超线程,有时称为同时多线程(simultaneous multi-threading),是一项允许一个 CPU 执行多个控制流的技术。它涉及 CPU 某些硬件有多个备份,比如程序计数器和寄存器文件;而其他的硬件部分只有一份,比如执行浮点算术运算的单元。常规的处理器需要大约 20 000 个时钟周期做不同线程间的转换,而超线程的处理器可以在单个周期的基础上决定要执行哪一个线程。这使得 CPU 能够更好地利用它的处理资源。例如,假设一个线程必须等到某些数据被装载到高速缓存中,那 CPU 就可以继续去执行另一个线程。 10 | 11 | ## 指令级并发 12 | 13 | 在较低的抽象层次上,现代处理器可以同时执行多条指令的属性称为指令级并行。实每条指令从开始到结束需要长得多的时间,大约 20 个或者更多的周期,但是处理器使用了非常多的聪明技巧来同时处理多达 100 条的指令。在流水线中,将执行一条指令所需要的活动划分成不同的步骤,将处理器的硬件组织成一系列的阶段,每个阶段执行一个步骤。这些阶段可以并行地操作,用来处理不同指令的不同部分。我们会看到一个相当简单的硬件设计,它能够达到接近于一个时钟周期一条指令的执行速率。如果处理器可以达到比一个周期一条指令更快的执行速率,就称之为超标量(Super Scalar)处理器。 14 | 15 | ## 单指令、多数据 16 | 17 | 在最低层次上,许多现代处理器拥有特殊的硬件,允许一条指令产生多个可以并行执行的操作,这种方式称为单指令、多数据,即 SIMD 并行。例如,较新的 Intel 和 AMD 处理器都具有并行地对 4 对单精度浮点数(C 数据类型 float)做加法的指令。 18 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCountMultipleCounters/src/main/scala/com/paulbutcher/Accumulator.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import akka.actor._ 12 | import collection.Map 13 | import collection.mutable.HashMap 14 | 15 | case class Counts(counts: Map[String, Int]) 16 | 17 | class Accumulator extends Actor { 18 | 19 | val counts = HashMap[String, Int]().withDefaultValue(0) 20 | 21 | def receive = { 22 | case Counts(partialCounts) => 23 | for ((word, count) <- partialCounts) 24 | counts(word) += count 25 | } 26 | 27 | override def postStop() { 28 | // for ((k, v) <- counts) 29 | // println(s"$k=$v") 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/WordCount/src/wordcount/core.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns wordcount.core 10 | (:require [wordcount.pages :refer :all] 11 | [wordcount.words :refer :all])) 12 | 13 | (defn count-words-sequential [pages] 14 | (frequencies (mapcat get-words pages))) 15 | 16 | (defn count-words-parallel [pages] 17 | (reduce (partial merge-with +) 18 | (pmap #(frequencies (get-words %)) pages))) 19 | 20 | (defn count-words [pages] 21 | (reduce (partial merge-with +) 22 | (pmap count-words-sequential (partition-all 100 pages)))) 23 | 24 | (defn -main [& args] 25 | (time (count-words (take 100000 (get-pages "enwiki.xml")))) 26 | (shutdown-agents)) 27 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCountFaultTolerant/src/main/scala/com/paulbutcher/Words.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import java.text.BreakIterator 12 | 13 | class Words(text: String) extends Iterator[String] { 14 | val wordBoundary = BreakIterator.getWordInstance 15 | wordBoundary.setText(text) 16 | var start = wordBoundary.first 17 | var end = wordBoundary.next 18 | 19 | def hasNext = end != BreakIterator.DONE 20 | 21 | def next() = { 22 | val s = text.subSequence(start, end) 23 | start = end 24 | end = wordBoundary.next 25 | s.toString 26 | } 27 | } 28 | 29 | object Words { 30 | def apply(text:String) = new Words(text) 31 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/WordCount/src/wordcount/http.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns wordcount.http 10 | (:require [clojure.core.async :as async :refer :all :exclude [map into reduce merge partition partition-by take]] 11 | [org.httpkit.client :as http]) 12 | (:import [java.net URL])) 13 | 14 | (defn report-error [response] 15 | (println "Error" (:status response) "retrieving URL:" (get-in response [:opts :url]))) 16 | 17 | (defn http-get [url] 18 | (let [ch (chan)] 19 | (http/get url (fn [response] 20 | (if (= 200 (:status response)) 21 | (put! ch response) 22 | (do (report-error response) (close! ch))))) 23 | ch)) 24 | -------------------------------------------------------------------------------- /02~并发模型/生产者与消费者/Disruptor/99~参考资料/2016~美团技术团队~高性能队列 Disruptor.md: -------------------------------------------------------------------------------- 1 | > [原文地址](https://tech.meituan.com/2016/11/18/disruptor.html),以下是文章的主要内容概述: 2 | > 3 | > ### 背景 4 | > 5 | > - Disruptor 是 LMAX 公司开发的高性能队列,用于解决内存队列延迟问题。 6 | > - 单线程能支撑每秒 600 万订单。 7 | > - 获得 2011 年 Oracle 官方 Duke 大奖。 8 | > 9 | > ### Java 内置队列问题 10 | > 11 | > - 介绍了 Java 内置队列及其性能问题,特别是 ArrayBlockingQueue 的加锁和伪共享问题。 12 | > 13 | > ### ArrayBlockingQueue 问题分析 14 | > 15 | > - 加锁影响性能,CAS 操作比加锁性能好。 16 | > - 伪共享问题,通过增加变量间隔解决。 17 | > 18 | > ### Disruptor 设计方案 19 | > 20 | > - 环形数组结构,无锁设计,元素位置快速定位。 21 | > 22 | > ### Disruptor 实现 23 | > 24 | > - 单生产者和多生产者情况下的读写流程。 25 | > - 使用 CAS 保证线程安全。 26 | > 27 | > ### Disruptor 性能 28 | > 29 | > - 提供了与 ArrayBlockingQueue 的性能对比数据,显示 Disruptor 吞吐量快 4~7 倍。 30 | > 31 | > ### 等待策略 32 | > 33 | > - 介绍了 Disruptor 中不同等待策略的适用场景。 34 | > 35 | > ### Log4j 2 应用场景 36 | > 37 | > - Log4j 2 采用 Disruptor 提高多线程并发场景下的性能。 38 | > 39 | > ### 性能差异 40 | > 41 | > - 对比了 loggers all async 和 Async Appender 的性能差异。 42 | > 43 | > ### 参考文档 44 | > 45 | > - 文章最后提供了参考文档链接。 46 | > 47 | > 文章详细介绍了 Disruptor 的设计原理、实现方式以及在实际应用中的性能表现,对于需要高性能队列解决方案的开发者来说,是一篇很有价值的资料。 48 | 49 | # 高性能队列 Disruptor 50 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCountMultipleCounters/src/main/scala/com/paulbutcher/Words.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import java.text.BreakIterator 12 | 13 | class Words(text: String) extends Iterator[String] { 14 | val wordBoundary = BreakIterator.getWordInstance 15 | wordBoundary.setText(text) 16 | var start = wordBoundary.first 17 | var end = wordBoundary.next 18 | 19 | def hasNext = end != BreakIterator.DONE 20 | 21 | def next() = { 22 | val s = text.subSequence(start, end) 23 | start = end 24 | end = wordBoundary.next 25 | s.toString 26 | } 27 | } 28 | 29 | object Words { 30 | def apply(text:String) = new Words(text) 31 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/WordCountReducer/src/wordcount/core.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns wordcount.core 10 | (:require [wordcount.pages :refer :all] 11 | [wordcount.words :refer :all] 12 | [clojure.core.reducers :as r] 13 | [foldable-seq.core :refer [foldable-seq]])) 14 | 15 | (defn frequencies-parallel [words] 16 | (r/fold (partial merge-with +) 17 | (fn [counts word] (assoc counts word (inc (get counts word 0)))) 18 | words)) 19 | 20 | (defn count-words [pages] 21 | (frequencies-parallel (r/mapcat get-words (foldable-seq pages)))) 22 | 23 | (defn -main [& args] 24 | (count-words (get-pages 10000 "enwiki.xml")) 25 | nil) 26 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Actors/hello_actors/hello_actors2.exs: -------------------------------------------------------------------------------- 1 | #--- 2 | # Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | # published by The Pragmatic Bookshelf. 4 | # Copyrights apply to this code. It may not be used to create training material, 5 | # courses, books, articles, and the like. Contact us if you are in doubt. 6 | # We make no guarantees that this code is fit for any purpose. 7 | # Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | #--- 9 | defmodule Talker do 10 | def loop do 11 | receive do 12 | {:greet, name} -> IO.puts("Hello #{name}") 13 | {:praise, name} -> IO.puts("#{name}, you're amazing") 14 | {:celebrate, name, age} -> IO.puts("Here's to another #{age} years, #{name}") 15 | {:shutdown} -> exit(:normal) 16 | end 17 | loop 18 | end 19 | end 20 | 21 | pid = spawn_link(&Talker.loop/0) 22 | 23 | send(pid, {:greet, "Huey"}) 24 | send(pid, {:praise, "Dewey"}) 25 | send(pid, {:celebrate, "Louie", 16}) 26 | send(pid, {:shutdown}) 27 | 28 | receive do 29 | {:EXIT, ^pid, reason} -> IO.puts("Talker has exited (#{reason})") 30 | end 31 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/Wizard/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject csp-wizard "1.0" 10 | :source-paths ["src-clj"] 11 | :dependencies [[org.clojure/clojure "1.5.1"] 12 | [org.clojure/clojurescript "0.0-2138"] 13 | [org.clojure/core.async "0.1.267.0-0d7780-alpha"] 14 | [compojure "1.1.6"] 15 | [ring/ring-jetty-adapter "1.1.8"]] 16 | :plugins [[lein-cljsbuild "1.0.1"]] 17 | :cljsbuild { 18 | :builds [{:source-paths ["src-cljs"] 19 | :compiler {:output-to "resources/public/js/main.js" 20 | :optimizations :whitespace 21 | :pretty-print true}}]} 22 | :main wizard.core) 23 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/Animation/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject csp-animation "1.0" 10 | :source-paths ["src-clj"] 11 | :dependencies [[org.clojure/clojure "1.5.1"] 12 | [org.clojure/clojurescript "0.0-2138"] 13 | [org.clojure/core.async "0.1.267.0-0d7780-alpha"] 14 | [compojure "1.1.6"] 15 | [ring/ring-jetty-adapter "1.1.8"]] 16 | :plugins [[lein-cljsbuild "1.0.1"]] 17 | :cljsbuild { 18 | :builds [{:source-paths ["src-cljs"] 19 | :compiler {:output-to "resources/public/js/main.js" 20 | :optimizations :whitespace 21 | :pretty-print true}}]} 22 | :main animation.core) 23 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/threading-essentials-course/src/main/java/com/islomar/NewThreadExample.java: -------------------------------------------------------------------------------- 1 | package com.kata; 2 | 3 | import java.time.LocalTime; 4 | 5 | public class NewThreadExample { 6 | 7 | public static void main(String... args) { 8 | Thread timeThread = new Thread(() -> { 9 | while(true) { 10 | System.out.println(LocalTime.now()); 11 | try { 12 | Thread.sleep(1000); 13 | } catch (InterruptedException e) { 14 | return; 15 | } 16 | } 17 | }, "timeThread"); 18 | timeThread.start(); 19 | 20 | Thread helloThread = new Thread(() -> { 21 | while(true) { 22 | System.out.println("Hello world"); 23 | try { 24 | Thread.sleep(5000); 25 | } catch (InterruptedException e) { 26 | return; 27 | } 28 | } 29 | }, "helloThread"); 30 | helloThread.start(); 31 | 32 | System.out.println("Main: I'm done, outta here!!"); 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Clojure/DiningPhilosphersAtom2/src/philosophers/util.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns philosophers.util) 10 | 11 | (defn swap-when! 12 | "If (pred current-value-of-atom) is true, atomically swaps the value 13 | of the atom to become (apply f current-value-of-atom args). Note that 14 | both pred and f may be called multiple times, and thus should be free 15 | of side-effects. Returns the value that was swapped in if the 16 | predicate was true, nil otherwise." 17 | [a pred f & args] 18 | (loop [] 19 | (let [old @a] 20 | (if (pred old) 21 | (let [new (apply f old args)] 22 | (if (compare-and-set! a old new) 23 | new 24 | (recur))) 25 | nil)))) 26 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/DiningPhilosophers/src/main/java/com/paulbutcher/DiningPhilosophers.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | public class DiningPhilosophers { 12 | 13 | public static void main(String[] args) throws InterruptedException { 14 | Philosopher[] philosophers = new Philosopher[5]; 15 | Chopstick[] chopsticks = new Chopstick[5]; 16 | 17 | for (int i = 0; i < 5; ++i) 18 | chopsticks[i] = new Chopstick(i); 19 | for (int i = 0; i < 5; ++i) { 20 | philosophers[i] = new Philosopher(chopsticks[i], chopsticks[(i + 1) % 5]); 21 | philosophers[i].start(); 22 | } 23 | for (int i = 0; i < 5; ++i) 24 | philosophers[i].join(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/Puzzle/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-puzzle 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Puzzle 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.Puzzle 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/DiningPhilosophersFixed/src/main/java/com/paulbutcher/DiningPhilosophers.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | public class DiningPhilosophers { 12 | 13 | public static void main(String[] args) throws InterruptedException { 14 | Philosopher[] philosophers = new Philosopher[5]; 15 | Chopstick[] chopsticks = new Chopstick[5]; 16 | 17 | for (int i = 0; i < 5; ++i) 18 | chopsticks[i] = new Chopstick(i); 19 | for (int i = 0; i < 5; ++i) { 20 | philosophers[i] = new Philosopher(chopsticks[i], chopsticks[(i + 1) % 5]); 21 | philosophers[i].start(); 22 | } 23 | for (int i = 0; i < 5; ++i) 24 | philosophers[i].join(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/HttpDownload/src/main/java/com/paulbutcher/HttpDownload.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | import java.net.URL; 12 | 13 | public class HttpDownload { 14 | 15 | public static void main(String[] args) throws Exception { 16 | URL from = new URL("http://download.wikimedia.org/enwiki/latest/enwiki-latest-pages-articles.xml.bz2"); 17 | Downloader downloader = new Downloader(from, "download.out"); 18 | downloader.start(); 19 | downloader.addListener(new ProgressListener() { 20 | public void onProgress(int n) { System.out.print("\r"+n); System.out.flush(); } 21 | public void onComplete(boolean success) {} 22 | }); 23 | downloader.join(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/Counting/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-counting 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Counting 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.Counting 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/HttpDownloadBetter/src/main/java/com/paulbutcher/HttpDownload.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | import java.net.URL; 12 | 13 | public class HttpDownload { 14 | 15 | public static void main(String[] args) throws Exception { 16 | URL from = new URL("http://download.wikimedia.org/enwiki/latest/enwiki-latest-pages-articles.xml.bz2"); 17 | Downloader downloader = new Downloader(from, "download.out"); 18 | downloader.start(); 19 | downloader.addListener(new ProgressListener() { 20 | public void onProgress(int n) { System.out.print("\r"+n); System.out.flush(); } 21 | public void onComplete(boolean success) {} 22 | }); 23 | downloader.join(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/HttpDownloadFixed/src/main/java/com/paulbutcher/HttpDownload.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | import java.net.URL; 12 | 13 | public class HttpDownload { 14 | 15 | public static void main(String[] args) throws Exception { 16 | URL from = new URL("http://download.wikimedia.org/enwiki/latest/enwiki-latest-pages-articles.xml.bz2"); 17 | Downloader downloader = new Downloader(from, "download.out"); 18 | downloader.start(); 19 | downloader.addListener(new ProgressListener() { 20 | public void onProgress(int n) { System.out.print("\r"+n); System.out.flush(); } 21 | public void onComplete(boolean success) {} 22 | }); 23 | downloader.join(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCount/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-word-count 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Word Count 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.WordCount 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/HelloClojureScript/project.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (defproject csp-hello-clojurescript "1.0" 10 | :source-paths ["src-clj"] 11 | :dependencies [[org.clojure/clojure "1.5.1"] 12 | [org.clojure/clojurescript "0.0-2138"] 13 | [org.clojure/core.async "0.1.267.0-0d7780-alpha"] 14 | [compojure "1.1.6"] 15 | [ring/ring-jetty-adapter "1.1.8"]] 16 | :plugins [[lein-cljsbuild "1.0.1"]] 17 | :cljsbuild { 18 | :builds [{:source-paths ["src-cljs"] 19 | :compiler {:output-to "resources/public/js/main.js" 20 | :optimizations :whitespace 21 | :pretty-print true}}]} 22 | :main hello-clojurescript.core) 23 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/channels/src/channels/core.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns channels.core 10 | (:require [clojure.core.async :as async :refer :all 11 | :exclude [map into reduce merge partition partition-by take]])) 12 | 13 | (defn readall!! [ch] 14 | (loop [coll []] 15 | (if-let [x (!! ch x)) 22 | (close! ch)) 23 | 24 | (defn go-add [x y] 25 | (! to (f x)) 32 | (recur)) 33 | (close! to)) 34 | to)) 35 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/DateFormatBug/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | fp-dateformat-bug 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | FP DateFormat Bug 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.DateFormatBug 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/EchoServer/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-echo-server 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Echo Server 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.EchoServer 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/HelloWorld/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-hello-world 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Hello World 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.HelloWorld 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/LinkedList/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-linked-list 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Linked List 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.LinkedList 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/CountingFixed/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-counting-fixed 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Counting Fixed 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.Counting 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/HttpDownload/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-http-download 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks HTTP Download 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.HttpDownload 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/HelloClojureScript/src-cljs/hello_clojurescript/core.cljs: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns hello-clojurescript.core 10 | (:require-macros [cljs.core.async.macros :refer [go]]) 11 | (:require [goog.dom :refer [append createDom getElement]] 12 | [cljs.core.async :refer [! primes prime) 21 | (recur (remove< (partial factor? prime) ch))) 22 | (close! primes)) 23 | primes)) 24 | 25 | (defn -main [limit] 26 | (let [primes (get-primes (Integer. limit))] 27 | (loop [] 28 | (when-let [prime ( 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-counting-better 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Counting Better 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.Counting 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/Interruptible/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-interruptible 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Interruptible 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.Interruptible 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/LambdaArchitecture/ExtractWikiText/src/wiki/pages.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns wiki.pages 10 | (:require [clojure.data.xml :as xml] 11 | [clojure.java.io :as io])) 12 | 13 | (defn- parent->children [parent tag] 14 | (filter #(= tag (:tag %)) (:content parent))) 15 | 16 | (defn- parent->child [parent tag] 17 | (first (parent->children parent tag))) 18 | 19 | (defn- content [tag] 20 | (first (:content tag))) 21 | 22 | (defn- text [page] 23 | (let [revision (parent->child page :revision) 24 | text (parent->child revision :text)] 25 | (content text))) 26 | 27 | (defn get-pages [filename] 28 | (let [in (io/reader filename) 29 | page-tags (parent->children (xml/parse in) :page)] 30 | (map text page-tags))) -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/Uninterruptible/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-uninterruptible 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Uninterruptible 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.Uninterruptible 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/WordCount/src/wordcount/pages.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns wordcount.pages 10 | (:require [clojure.data.xml :as xml] 11 | [clojure.java.io :as io])) 12 | 13 | (defn- parent->children [parent tag] 14 | (filter #(= tag (:tag %)) (:content parent))) 15 | 16 | (defn- parent->child [parent tag] 17 | (first (parent->children parent tag))) 18 | 19 | (defn- content [tag] 20 | (first (:content tag))) 21 | 22 | (defn- text [page] 23 | (let [revision (parent->child page :revision) 24 | text (parent->child revision :text)] 25 | (content text))) 26 | 27 | (defn get-pages [filename] 28 | (let [in (io/reader filename) 29 | page-tags (parent->children (xml/parse in) :page)] 30 | (map text page-tags))) -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/EchoServerBetter/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-echo-server-better 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Echo Server Better 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.EchoServer 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/HttpDownloadBetter/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-http-download-better 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks HTTP Download Better 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.HttpDownload 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/HttpDownloadFixed/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-http-download-fixed 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks HTTP Download Fixed 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.HttpDownload 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCountFaultTolerant/src/main/scala/com/paulbutcher/Accumulator.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import akka.actor._ 12 | import collection.Map 13 | import collection.mutable.{HashMap, Set} 14 | 15 | case class Counts(id: Int, counts: Map[String, Int]) 16 | 17 | class Accumulator(parser: ActorRef) extends Actor { 18 | 19 | val counts = HashMap[String, Int]().withDefaultValue(0) 20 | val processedIds = Set[Int]() 21 | 22 | def receive = { 23 | case Counts(id, partialCounts) => 24 | if (!processedIds.contains(id)) { 25 | for ((word, count) <- partialCounts) 26 | counts(word) += count 27 | processedIds += id 28 | parser ! Processed(id) 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/DiningPhilosophers/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-dining-philosophers 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Dining Philosophers 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.DiningPhilosophers 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/TranscriptHandler/src/server/core.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns server.core 10 | (:require [compojure.core :refer :all] 11 | [compojure.handler :refer [site]] 12 | [ring.util.response :refer [response]] 13 | [ring.adapter.jetty :refer [run-jetty]])) 14 | 15 | (def snippets (repeatedly promise)) 16 | 17 | (future 18 | (doseq [snippet (map deref snippets)] 19 | (println snippet))) 20 | 21 | (defn accept-snippet [n text] 22 | (deliver (nth snippets n) text)) 23 | 24 | (defroutes app-routes 25 | (PUT "/snippet/:n" [n :as {:keys [body]}] 26 | (accept-snippet (Integer. n) (slurp body)) 27 | (response "OK"))) 28 | 29 | (defn -main [& args] 30 | (run-jetty (site app-routes) {:port 3000})) 31 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCountFaultTolerant/src/main/scala/com/paulbutcher/Counter.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import akka.actor._ 12 | import collection.mutable.HashMap 13 | 14 | case class ParserAvailable(parser: ActorRef) 15 | case class Batch(id: Int, pages: Seq[Page], accumulator: ActorRef) 16 | 17 | class Counter extends Actor { 18 | 19 | def receive = { 20 | case ParserAvailable(parser) => parser ! RequestBatch 21 | 22 | case Batch(id, pages, accumulator) => 23 | sender ! RequestBatch 24 | val counts = HashMap[String, Int]().withDefaultValue(0) 25 | for (page <- pages) 26 | for (word <- Words(page.text)) 27 | counts(word) += 1 28 | accumulator ! Counts(id, counts) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/BuggyCache/src/main/scala/com/paulbutcher/BuggyCache.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import akka.actor._ 12 | import collection.mutable.HashMap 13 | 14 | case class Put(key: String, value: String) 15 | case class Get(key: String) 16 | case object ReportSize 17 | 18 | class BuggyCache extends Actor { 19 | 20 | val cache = HashMap[String, String]() 21 | var size = 0 22 | 23 | def receive = { 24 | case Put(key, value) => 25 | cache(key) = value 26 | size += value.length 27 | 28 | case Get(key) => sender ! Result(cache(key)) 29 | 30 | case ReportSize => sender ! Result(size) 31 | } 32 | 33 | override def postRestart(reason: Throwable) { 34 | println("BuggyCache has restarted") 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/DiningPhilosophersFixed/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-dining-philosophers-fixed 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Dining Philosophers Fixed 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.DiningPhilosophers 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountProducerConsumer/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-word-count-producer-consumer 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Word Count Producer Consumer 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.WordCount 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountConcurrentHashMap/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-word-count-concurrent-hash-map 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Word Count ConcurrentHashMap 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.WordCount 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/HelloCluster/src/main/scala/com/paulbutcher/TestActor.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import akka.actor._ 12 | import akka.cluster.ClusterEvent._ 13 | 14 | case class HelloFrom(actor: ActorRef) 15 | 16 | class TestActor extends Actor { 17 | 18 | def receive = { 19 | case MemberUp(member) => 20 | println(s"Member is up: $member") 21 | val remotePath = RootActorPath(member.address) / "user" / "test-actor" 22 | val remote = context.actorFor(remotePath) 23 | remote ! HelloFrom(self) 24 | context.watch(remote) 25 | 26 | case HelloFrom(actor) => println(s"Hello from: $actor") 27 | case Terminated(actor) => println(s"Terminated: $actor") 28 | case event => println(s"Event: $event") 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/DiningPhilosophersTimeout/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-dining-philosophers-timeout 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Dining Philosophers Timeout 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.DiningPhilosophers 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountSynchronizedHashMap/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-word-count-synchronized-hash-map 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Word Count Synchronized HashMap 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.WordCount 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/DiningPhilosophersCondition/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-dining-philosophers-condition 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Dining Philosophers Condition 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.DiningPhilosophers 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/DiningPhilosophersTimeout/src/main/java/com/paulbutcher/DiningPhilosophers.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | import java.util.concurrent.locks.ReentrantLock; 12 | 13 | public class DiningPhilosophers { 14 | 15 | public static void main(String[] args) throws InterruptedException { 16 | Philosopher[] philosophers = new Philosopher[5]; 17 | ReentrantLock[] chopsticks = new ReentrantLock[5]; 18 | 19 | for (int i = 0; i < 5; ++i) 20 | chopsticks[i] = new ReentrantLock(); 21 | for (int i = 0; i < 5; ++i) { 22 | philosophers[i] = new Philosopher(chopsticks[i], chopsticks[(i + 1) % 5]); 23 | philosophers[i].start(); 24 | } 25 | for (int i = 0; i < 5; ++i) 26 | philosophers[i].join(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/WordCountBatchConcurrentHashMap/pom.xml: -------------------------------------------------------------------------------- 1 | 9 | 11 | 4.0.0 12 | 13 | com.paulbutcher 14 | threads-locks-word-count-batch-concurrent-hash-map 15 | 1.0-SNAPSHOT 16 | jar 17 | 18 | ThreadsLocks Word Count Batch ConcurrentHashMap 19 | 20 | 21 | UTF-8 22 | com.paulbutcher.WordCount 23 | 24 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/CSP/SieveTimeout/src/sieve/core.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns sieve.core 10 | (:require [clojure.core.async :as async :refer :all :exclude [map into reduce merge partition partition-by take]])) 11 | 12 | (defn factor? [x y] 13 | (zero? (mod y x))) 14 | 15 | (defn get-primes [] 16 | (let [primes (chan) 17 | numbers (to-chan (iterate inc 2))] 18 | (go-loop [ch numbers] 19 | (when-let [prime (! primes prime) 21 | (recur (remove< (partial factor? prime) ch))) 22 | (close! primes)) 23 | primes)) 24 | 25 | (defn -main [seconds] 26 | (let [primes (get-primes) 27 | limit (timeout (* (Integer. seconds) 1000))] 28 | (loop [] 29 | (alt!! :priority true 30 | limit nil 31 | primes ([prime] (println prime) (recur)))))) 32 | -------------------------------------------------------------------------------- /02~并发模型/Actor/README.md: -------------------------------------------------------------------------------- 1 | # Actor 2 | 3 | Actor 模型是自 1970 年以来一直在发展的一种普遍存在的通用消息传递编程模型,如今已用于构建大规模可伸缩系统。20 世纪 70 年代 Carl Hewitt 首次提出了 Actor 模型,而后 Erlang 为布道 Actor 做了最大的贡献,比如 Erlang 的创始人 Joe Armstrong 也是“任其崩溃”哲学的先驱。Actor 模型可以看做面向对象模型在并发编程领域的扩展,其精心设计了消息传输和封装的机制,强调了面向对象的精髓。 4 | 5 | Actor 模型是一个概念模型,用于处理并发计算。Actor 是分布式存在的内存状态及单线程计算单元,一个 ID 对应的 Actor 只会在集群种存在一个(有状态的 Actor 在集群中一个 ID 只会存在一个实例,无状态的可配置为根据流量存在多个),使用者只需要通过 ID 就能随时访问不需要关注该 Actor 在集群的什么位置。单线程计算单元保证了消息的顺序到达,不存在 Actor 内部状态竞用问题。 6 | 7 | 尽管 Actor 模型的起源可追溯到 1970 年代,但正如该领域最近发表的论文和系统所展示的那样,它仍在开发中,并已被纳入当今的编程语言中。有一些健壮的工业实力参与者系统正在为大型可扩展分布式系统提供动力。例如,Akka 已用于为 PayPal 的数十亿笔交易提供服务((Sucharitakul,2016 年)。Erlang 已用于为 WhatsApp 的亿万用户发送消息,(Reed,2012 年),而 Orleans 已用于为 Halo 4 的数百万玩家提供服务 。(McCaffrey,2015 年)围绕监视,处理容错和管理参与者生命周期,有几种不同的方法来构建工业参与者框架。 8 | 9 | # 最初的 Actor 模型 10 | 11 | Actor 由 3 部分组成:状态(State)、行为(Behavior)、邮箱(Mailbox): 12 | 13 | - State 是指 Actor 对象的变量信息,存在于 Actor 之中,Actor 之间不共享内存数据,Actor 只会在接收到消息后,调用自己的方法改变自己的 state,从而避免并发条件下的死锁等问题; 14 | - Behavior 是指 Actor 的计算行为逻辑; 15 | - 邮箱建立 Actor 之间的联系,一个 Actor 发送消息后,接收消息的 Actor 将消息放入邮箱中等待处理,邮箱内部通过队列实现,消息传递通过异步方式进行。 16 | 17 | ![Actor 示意图](https://s3.ax1x.com/2021/01/28/y9KVmT.png) 18 | 19 | # Links 20 | 21 | - http://dist-prog-book.com/chapter/3/message-passing.html#introduction 22 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/CountingBetter/src/main/java/com/paulbutcher/Counting.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | import java.util.concurrent.atomic.AtomicInteger; 12 | 13 | public class Counting { 14 | 15 | public static void main(String[] args) throws InterruptedException { 16 | 17 | final AtomicInteger counter = new AtomicInteger(); 18 | 19 | class CountingThread extends Thread { 20 | public void run() { 21 | for(int x = 0; x < 10000; ++x) 22 | counter.incrementAndGet(); 23 | } 24 | } 25 | 26 | CountingThread t1 = new CountingThread(); 27 | CountingThread t2 = new CountingThread(); 28 | 29 | t1.start(); t2.start(); 30 | t1.join(); t2.join(); 31 | 32 | System.out.println(counter.get()); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/FunctionalProgramming/WordCountReducer/src/wordcount/pages.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns wordcount.pages 10 | (:require [clojure.data.xml :as xml] 11 | [clojure.java.io :as io])) 12 | 13 | (defn- parent->children [parent tag] 14 | (filter #(= tag (:tag %)) (:content parent))) 15 | 16 | (defn- parent->child [parent tag] 17 | (first (parent->children parent tag))) 18 | 19 | (defn- content [tag] 20 | (first (:content tag))) 21 | 22 | (defn- text [page] 23 | (let [revision (parent->child page :revision) 24 | text (parent->child revision :text)] 25 | (content text))) 26 | 27 | (defn get-pages [n filename] 28 | (let [in (io/reader filename) 29 | content (:content (xml/parse in)) 30 | pages (take n (filter #(= :page (:tag %)) content))] 31 | (map text pages))) 32 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/threading-essentials-course/src/main/java/com/islomar/ParallelSorting.java: -------------------------------------------------------------------------------- 1 | package com.islomar; 2 | 3 | import java.util.Arrays; 4 | import java.util.concurrent.ThreadLocalRandom; 5 | import java.util.function.Consumer; 6 | 7 | 8 | public class ParallelSorting { 9 | public static void main(String... args) { 10 | int[] numbers = ThreadLocalRandom.current() 11 | .ints(100_000_000) 12 | .toArray(); 13 | testSorting(numbers); 14 | } 15 | 16 | private static void testSorting(int[] numbers) { 17 | int[] numbersSequential = numbers.clone(); 18 | int[] numbersParallel = numbers.clone(); 19 | 20 | sort("sequential", numbersSequential, Arrays::sort); 21 | sort("parallel", numbersParallel, Arrays::parallelSort); 22 | } 23 | 24 | private static void sort(String description, int[] numbers, Consumer sortingAlgorithm) { 25 | long time = System.currentTimeMillis(); 26 | try { 27 | sortingAlgorithm.accept(numbers); 28 | } finally { 29 | time = System.currentTimeMillis() - time; 30 | System.out.println(description + " - time = " + time + " ms"); 31 | } 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/Puzzle/src/main/java/com/paulbutcher/Puzzle.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | public class Puzzle { 12 | 13 | static boolean answerReady = false; 14 | static int answer = 0; 15 | 16 | static Thread t1 = new Thread() { 17 | public void run() { 18 | answer = 42; 19 | answerReady = true; 20 | } 21 | }; 22 | 23 | static Thread t2 = new Thread() { 24 | public void run() { 25 | if (answerReady) 26 | System.out.println("The meaning of life is: " + answer); 27 | else 28 | System.out.println("I don't know the answer"); 29 | } 30 | }; 31 | 32 | public static void main(String[] args) throws InterruptedException { 33 | t1.start(); t2.start(); 34 | t1.join(); t2.join(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/BuggyCache/src/main/scala/com/paulbutcher/Master.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import akka.actor._ 12 | import akka.actor.SupervisorStrategy._ 13 | import java.util.NoSuchElementException 14 | 15 | case class Result(result: Any) 16 | 17 | class Master extends Actor { 18 | 19 | val cache = context.actorOf(Props[BuggyCache], "cache") 20 | 21 | def receive = { 22 | case Put(key, value) => cache ! Put(key, value) 23 | case Get(key) => cache ! Get(key) 24 | case ReportSize => cache ! ReportSize 25 | case Result(result) => println(result) 26 | } 27 | 28 | override val supervisorStrategy = OneForOneStrategy() { 29 | case _: NoSuchElementException => Resume 30 | case _: NullPointerException => Restart 31 | case _ => Escalate 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/Clojure/TournamentServer/src/server/core.clj: -------------------------------------------------------------------------------- 1 | ;--- 2 | ; Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | ; published by The Pragmatic Bookshelf. 4 | ; Copyrights apply to this code. It may not be used to create training material, 5 | ; courses, books, articles, and the like. Contact us if you are in doubt. 6 | ; We make no guarantees that this code is fit for any purpose. 7 | ; Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ;--- 9 | (ns server.core 10 | (:require [compojure.core :refer :all] 11 | [compojure.handler :refer [site]] 12 | [ring.util.response :refer [response status]] 13 | [ring.adapter.jetty :refer [run-jetty]] 14 | [cheshire.core :as json])) 15 | 16 | (def players (atom ())) 17 | 18 | (defn list-players [] 19 | (response (json/encode @players))) 20 | 21 | (defn create-player [player-name] 22 | (swap! players conj player-name) 23 | (status (response "") 201)) 24 | 25 | (defroutes app-routes 26 | (GET "/players" [] (list-players)) 27 | (PUT "/players/:player-name" [player-name] (create-player player-name))) 28 | 29 | (defn -main [& args] 30 | (run-jetty (site app-routes) {:port 3000})) 31 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/DiningPhilosophersCondition/src/main/java/com/paulbutcher/DiningPhilosophers.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | import java.util.concurrent.locks.ReentrantLock; 12 | 13 | public class DiningPhilosophers { 14 | 15 | public static void main(String[] args) throws InterruptedException { 16 | Philosopher[] philosophers = new Philosopher[5]; 17 | ReentrantLock table = new ReentrantLock(); 18 | 19 | for (int i = 0; i < 5; ++i) 20 | philosophers[i] = new Philosopher(table); 21 | for (int i = 0; i < 5; ++i) { 22 | philosophers[i].setLeft(philosophers[(i + 4) % 5]); 23 | philosophers[i].setRight(philosophers[(i + 1) % 5]); 24 | philosophers[i].start(); 25 | } 26 | for (int i = 0; i < 5; ++i) 27 | philosophers[i].join(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /02~并发模型/流水线模型/流水线模型.md: -------------------------------------------------------------------------------- 1 | # 流水线模型 2 | 3 | 流水线模型,特点是无状态线程,无状态也意味着无需竞争共享资源,无需等待,也就是非阻塞模型。流水线模型顾名思义就是流水线上有多个环节,每个环节完成自己的工作后就交给下一个环节,无需等待上游,周而复始的完成自己岗位上的一亩三分地就行。各个环节之间交付无需等待,完成即可交付。 4 | 5 | ![](https://s2.ax1x.com/2019/09/02/nPEwnJ.png) 6 | 7 | 而工厂的流水线也不止一条,所以有多条流水线同时工作,不同岗位的生产效率是不一样的,所以不同流水线之间也可以发生协同。 8 | 9 | ![](https://s2.ax1x.com/2019/09/02/nPVp40.png) 10 | 11 | 我们说流水线模型也称为响应式模型或者事件驱动模型,其实就是流水线上上游岗位完成生产就通知下游岗位,所以完成了一个事件的通知,每完成一次就通知一下,就是响应式的意思。流水线模型总体的思想就是纵向切分任务,把任务里面耗时过久的环节单独隔离出来,避免完成一个任务需要耗费等待的时间。在实现上又分为 Actors 和 Channels 模型。 12 | 13 | - Actors 14 | 15 | ![](https://s2.ax1x.com/2019/09/02/nPV3Ke.png) 16 | 17 | - Channels 18 | 19 | ![](https://s2.ax1x.com/2019/09/02/nPVDKg.png) 20 | 21 | 由于各个环节直接不直接交互,所以上下游之间并不知道对方是谁,好比不同环节直接用的是几条公共的传送带来接收物品,各自只需要把完成后的半成品扔到传送带,即使后面流水线优化了,去掉中间的环节,对于个体岗位来说也是无感知的,它只是周而复始的从传送带拿物品来加工。 22 | 23 | ## 优劣分析 24 | 25 | 流水线的优势如下: 26 | 27 | - 无共享状态:无需考虑资源抢占,死锁等问题 28 | - 独享内存:worker 可以持有内存,合并多次操作到内存后再持久化,提升效率 29 | - 贴合底层:单线程模式贴合硬件运行流程,便于代码维护 30 | - 任务顺序可预知 31 | 32 | 其缺陷在于: 33 | 34 | - 不够直观:一个任务被拆分为流水线上多个环节,代码层面难以直观理解业务逻辑 35 | 36 | 由于流水线模式跟人类的顺序执行思维不一样,比较费解,基于 Java 的 Akka/Reator/Vert.x/Play/Qbit 框架,或者 Golang、Node 等语言或者框架就让我们编码的时候像写传统的多线程代码一样,而运行起来又是流水线模式。 37 | 38 | 其实流水线模型背后用的也还是多线程来实现,只不过对于传统多线程模式下我们需要小心翼翼来处理跟踪资源共享问题,而流水线模式把以前一个线程做的事情拆成多个,每一个环节再用一条线程来完成,避免共享,线程直接通过管道传输消息。 39 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/MatrixMultiplication/matrix_multiplication.cl: -------------------------------------------------------------------------------- 1 | //--- 2 | // Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | // published by The Pragmatic Bookshelf. 4 | // Copyrights apply to this code. It may not be used to create training material, 5 | // courses, books, articles, and the like. Contact us if you are in doubt. 6 | // We make no guarantees that this code is fit for any purpose. 7 | // Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | //--- 9 | __kernel void matrix_multiplication(uint widthA, 10 | __global const float* inputA, 11 | __global const float* inputB, 12 | __global float* output) { 13 | 14 | int i = get_global_id(0); 15 | int j = get_global_id(1); 16 | 17 | // Note that: 18 | // outputWidth == widthB 19 | // outputHeight == heightA 20 | // widthA == heightB 21 | int outputWidth = get_global_size(0); 22 | int outputHeight = get_global_size(1); 23 | int widthB = outputWidth; 24 | 25 | float total = 0.0; 26 | for (int k = 0; k < widthA; ++k) { 27 | total += inputA[j * widthA + k] * inputB[k * widthB + i]; 28 | } 29 | output[j * outputWidth + i] = total; 30 | } 31 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/DataParallelism/MatrixMultiplicationProfiled/matrix_multiplication.cl: -------------------------------------------------------------------------------- 1 | //--- 2 | // Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | // published by The Pragmatic Bookshelf. 4 | // Copyrights apply to this code. It may not be used to create training material, 5 | // courses, books, articles, and the like. Contact us if you are in doubt. 6 | // We make no guarantees that this code is fit for any purpose. 7 | // Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | //--- 9 | __kernel void matrix_multiplication(uint widthA, 10 | __global const float* inputA, 11 | __global const float* inputB, 12 | __global float* output) { 13 | 14 | int i = get_global_id(0); 15 | int j = get_global_id(1); 16 | 17 | // Note that: 18 | // outputWidth == widthB 19 | // outputHeight == heightA 20 | // widthA == heightB 21 | int outputWidth = get_global_size(0); 22 | int outputHeight = get_global_size(1); 23 | int widthB = outputWidth; 24 | 25 | float total = 0.0; 26 | for (int k = 0; k < widthA; ++k) { 27 | total += inputA[j * widthA + k] * inputB[k * widthB + i]; 28 | } 29 | output[j * outputWidth + i] = total; 30 | } 31 | -------------------------------------------------------------------------------- /03~并发网络 IO/非阻塞与异步.md: -------------------------------------------------------------------------------- 1 | # 非阻塞与异步 2 | 3 | 在标准的网络服务器的构建中,IO 模式会按照 Blocking/Non-Blocking、Synchronous/Asynchronous 这两个标准进行分类,其中 Blocking 与 Synchronous 大同小异,而 NIO 与 Async 的区别在于 NIO 强调的是轮询(Polling),而 Async 强调的是通知(Notification)。譬如在一个典型的单进程单线程 Socket 接口中,阻塞型的接口必须在上一个 Socket 连接关闭之后才能接入下一个 Socket 连接。而对于 NIO 的 Socket 而言,服务端应用会从内核获取到一个特殊的 "Would Block" 错误信息,但是并不会阻塞到等待发起请求的 Socket 客户端停止。 4 | 5 | ![非阻塞与异步对比](https://pic.imgdb.cn/item/60861c63d1a9ae528f94e8b5.jpg) 6 | 7 | 一般来说,在 Linux 系统中可以通过调用独立的 `select` 或者 `epoll` 方法来遍历所有读取好的数据,并且进行写操作。而对于异步 Socket 而言(譬如 Windows 中的 Sockets 或者 .Net 中实现的 Sockets 模型),服务端应用会告诉 IO Framework 去读取某个 Socket 数据,在数据读取完毕之后 IO Framework 会自动地调用你的回调(也就是通知应用程序本身数据已经准备好了)。以 IO 多路复用中的 Reactor 与 Proactor 模型为例,非阻塞的模型是需要应用程序本身处理 IO 的,而异步模型则是由 Kernel 或者 Framework 将数据准备好读入缓冲区中,应用程序直接从缓冲区读取数据。 8 | 9 | - 同步阻塞:在此种方式下,用户进程在发起一个 IO 操作以后,必须等待 IO 操作的完成,只有当真正完成了 IO 操作以后,用户进程才能运行。 10 | 11 | - 同步非阻塞:在此种方式下,用户进程发起一个 IO 操作以后边可返回做其它事情,但是用户进程需要时不时的询问 IO 操作是否就绪,这就要求用户进程不停的去询问,从而引入不必要的 CPU 资源浪费。 12 | 13 | - 异步非阻塞:在此种模式下,用户进程只需要发起一个 IO 操作然后立即返回,等 IO 操作真正的完成以后,应用程序会得到 IO 操作完成的通知,此时用户进程只需要对数据进行处理就好了,不需要进行实际的 IO 读写操作,因为真正的 IO 读取或者写入操作已经由内核完成了。 14 | 15 | 而在并发 IO 的问题中,较常见的就是所谓的 C10K 问题,即有 10000 个客户端需要连上一个服务器并保持 TCP 连接,客户端会不定时的发送请求给服务器,服务器收到请求后需及时处理并返回结果。 16 | 17 | # Links 18 | 19 | - https://cubox.pro/c/v1Ssyx 怎样理解阻塞非阻塞与同步异步的区别? 20 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/Lifecycle/src/main/scala/com/paulbutcher/TestActor.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import akka.actor._ 12 | 13 | case class CreateChild(name: String) 14 | case class Divide(x: Int, y: Int) 15 | 16 | class TestActor extends Actor { 17 | 18 | def receive = { 19 | case CreateChild(name) => context.actorOf(Props[TestActor], name) 20 | case Divide(x, y) => log(s"$x / $y = ${x / y}") 21 | } 22 | 23 | override def preStart() { log(s"preStart") } 24 | 25 | override def preRestart(reason: Throwable, message: Option[Any]) { 26 | log(s"preRestart ($reason, $message)") 27 | } 28 | 29 | override def postRestart(reason: Throwable) { log(s"postRestart ($reason)") } 30 | 31 | override def postStop() { log(s"postStop") } 32 | 33 | def log(message: String) { println(s"${self.path.name}: $message") } 34 | } 35 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all 2 | * 3 | 4 | # Unignore all with extensions 5 | !*.* 6 | 7 | # Unignore all dirs 8 | !*/ 9 | 10 | .DS_Store 11 | 12 | # Logs 13 | logs 14 | *.log 15 | npm-debug.log* 16 | yarn-debug.log* 17 | yarn-error.log* 18 | 19 | # Runtime data 20 | pids 21 | *.pid 22 | *.seed 23 | *.pid.lock 24 | 25 | # Directory for instrumented libs generated by jscoverage/JSCover 26 | lib-cov 27 | 28 | # Coverage directory used by tools like istanbul 29 | coverage 30 | 31 | # nyc test coverage 32 | .nyc_output 33 | 34 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 35 | .grunt 36 | 37 | # Bower dependency directory (https://bower.io/) 38 | bower_components 39 | 40 | # node-waf configuration 41 | .lock-wscript 42 | 43 | # Compiled binary addons (https://nodejs.org/api/addons.html) 44 | build/Release 45 | 46 | # Dependency directories 47 | node_modules/ 48 | jspm_packages/ 49 | 50 | # TypeScript v1 declaration files 51 | typings/ 52 | 53 | # Optional npm cache directory 54 | .npm 55 | 56 | # Optional eslint cache 57 | .eslintcache 58 | 59 | # Optional REPL history 60 | .node_repl_history 61 | 62 | # Output of 'npm pack' 63 | *.tgz 64 | 65 | # Yarn Integrity file 66 | .yarn-integrity 67 | 68 | # dotenv environment variables file 69 | .env 70 | 71 | # next.js build output 72 | .next 73 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/HelloActors/src/main/scala/com/paulbutcher/HelloActors.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import akka.actor._ 12 | 13 | case class Greet(name: String) 14 | case class Praise(name: String) 15 | case class Celebrate(name: String, age: Int) 16 | 17 | class Talker extends Actor { 18 | 19 | def receive = { 20 | case Greet(name) => println(s"Hello $name") 21 | case Praise(name) => println(s"$name, you're amazing") 22 | case Celebrate(name, age) => println(s"Here's to another $age years, $name") 23 | } 24 | } 25 | 26 | object HelloActors extends App { 27 | 28 | val system = ActorSystem("HelloActors") 29 | 30 | val talker = system.actorOf(Props[Talker], "talker") 31 | 32 | talker ! Greet("Huey") 33 | talker ! Praise("Dewey") 34 | talker ! Celebrate("Louie", 16) 35 | 36 | Thread.sleep(1000) 37 | 38 | system.shutdown 39 | } 40 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/Counting/src/main/java/com/paulbutcher/Counting.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | public class Counting { 12 | 13 | public static void main(String[] args) throws InterruptedException { 14 | 15 | class Counter { 16 | private int count = 0; 17 | public void increment() { ++count; } 18 | public int getCount() { return count; } 19 | } 20 | final Counter counter = new Counter(); 21 | 22 | class CountingThread extends Thread { 23 | public void run() { 24 | for(int x = 0; x < 10000; ++x) 25 | counter.increment(); 26 | } 27 | } 28 | 29 | CountingThread t1 = new CountingThread(); 30 | CountingThread t2 = new CountingThread(); 31 | 32 | t1.start(); t2.start(); 33 | t1.join(); t2.join(); 34 | 35 | System.out.println(counter.getCount()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/01~ThreadsLocks/CountingFixed/src/main/java/com/paulbutcher/Counting.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher; 10 | 11 | public class Counting { 12 | 13 | public static void main(String[] args) throws InterruptedException { 14 | 15 | class Counter { 16 | private int count = 0; 17 | public synchronized void increment() { ++count; } 18 | public int getCount() { return count; } 19 | } 20 | final Counter counter = new Counter(); 21 | 22 | class CountingThread extends Thread { 23 | public void run() { 24 | for(int x = 0; x < 10000; ++x) 25 | counter.increment(); 26 | } 27 | } 28 | 29 | CountingThread t1 = new CountingThread(); 30 | CountingThread t2 = new CountingThread(); 31 | 32 | t1.start(); t2.start(); 33 | t1.join(); t2.join(); 34 | 35 | System.out.println(counter.getCount()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /99~参考资料/2013~《七周七并发模型》/codes/ActorsScala/WordCountMultipleCounters/src/main/scala/com/paulbutcher/Master.scala: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "Seven Concurrency Models in Seven Weeks", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/pb7con for more book information. 8 | ***/ 9 | package com.paulbutcher 10 | 11 | import akka.actor._ 12 | import akka.routing.{Broadcast, RoundRobinRouter} 13 | 14 | class Master extends Actor { 15 | 16 | val accumulator = context.actorOf(Props[Accumulator], "accumulator") 17 | val counters = context.actorOf( 18 | Props(new Counter(accumulator)).withRouter(RoundRobinRouter(4)), 19 | "counter") 20 | val parser = context.actorOf(Props(new Parser(counters)), "parser") 21 | 22 | override def preStart { 23 | context.watch(accumulator) 24 | context.watch(counters) 25 | context.watch(parser) 26 | } 27 | 28 | def receive = { 29 | case Terminated(`parser`) => counters ! Broadcast(PoisonPill) 30 | case Terminated(`counters`) => accumulator ! PoisonPill 31 | case Terminated(`accumulator`) => context.system.shutdown 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /02~并发模型/结构化并发/README.md: -------------------------------------------------------------------------------- 1 | # 结构化并发 2 | 3 | 在多线程模型中,下面几个问题给程序员带来了更多复杂性和更重的心智负担。 4 | 5 | - 这个任务什么时候开始,什么时候结束? 6 | - 怎么做到当所有子任务都结束,主任务再结束? 7 | - 假如某个子任务失败,主任务如何终止掉其他子任务? 8 | - 如何保证所有子任务在某个特定的超时时间内返回,无论它成功还是失败? 9 | - 更进一步,如何保证主任务在规定的时间内返回,无论其成功还是失败,同时终止掉它产生的所有子任务? 10 | - 主任务已经结束了,子任务还在运行,是不是存在资源泄漏? 11 | 12 | 有没有一种编程范式,既可以解决这些问题,又具有相对比较低的认知门槛,同时也不需要像 Golang Context 那样侵入应用程序的接口?结构化并发(Structured Concurrency) 就是这样一种并发编程范式。 13 | 14 | 2016 年,ZerMQ 的作者 Martin Sústrik 在他的文章[5]中第一次形式化的提出结构化并发这个概念。2018 年 Nathaniel J. Smith (njs) 在 Python 中实现了这一范式 - trio[6],并在 Notes on structured concurrency, or: Go statement considered harmful[7] 一文中进一步阐述了 Structured Concurrency。同时期,Roman Elizarov 也提出了相同的理念[8],并在 Kotlin 中实现了大家熟知的 kotlinx.coroutine[9]。2019 年,OpenJDK loom project 也开始引入 structured concurrency,作为其轻量级线程和协程的一部分。 15 | 16 | Structured Concurrency 核心在于通过一种 structured 的方法实现并发程序,用具有明确入口点和出口点的控制流结构来封装并发“线程”(可以是系统级线程也可以是用户级线程,也就是协程,甚至可以是进程)的执行,确保所有派生“线程”在出口之前完成。如下例: 17 | 18 | ```go 19 | func main_func() { 20 | go myfunc() 21 | go anotherfunc() 22 | ... 23 | } 24 | ``` 25 | 26 | ![](https://s2.ax1x.com/2020/02/20/3ZtNA1.png) 27 | 28 | 假设上图中的代码具有 structured concurrency 特性(这里用的是 golang 的语法来展示)。main_func 里,创建了两个子任务:myfunc(), anotherfunc(),这里的 func 是一个控制流结构,入口就是 func 调用开始,出口是 func 调用结束,派生出来的两个子任务需要在 main_func 调用结束之前先完成。当 main_func 结束,它涉及到的资源也都会被释放掉。外部调用者无法也无需感知 main_func 里面到底是串行的还是并行的,它只需要调用 main_func,然后等待它结束即可。这就是所谓的 Structured。 29 | -------------------------------------------------------------------------------- /02~并发模型/生产者与消费者/Disruptor/99~参考资料/2020~初识 Disruptor 框架与广播模式.md: -------------------------------------------------------------------------------- 1 | > [原文地址](https://github.com/yuanmabiji/Java-SourceCode-Blogs/blob/master/Disruptor/%E5%88%9D%E8%AF%86Disruptor%E6%A1%86%E6%9E%B6.md) 2 | > 以下是该文档的主要内容概述: 3 | > 4 | > ### 1. 前言 5 | > 6 | > - **LMAX 交易平台**:运行在 JVM 平台上的零售金融交易平台,单个线程每秒处理 6 百万订单。 7 | > - **Disruptor 框架**:支持高并发性能,2011 年获得 Duke’s 程序框架创新奖。 8 | > - **核心数据结构**:Ring Buffer(环形数组),实现无锁的并发操作。 9 | > 10 | > ### 2. Disruptor 框架特点 11 | > 12 | > 1. **预加载内存**:使用内存池。 13 | > 2. **无锁化**:避免锁机制,提高并发性能。 14 | > 3. **单线程写**:每个生产者独立写入数据。 15 | > 4. **消除伪共享**:通过填充缓存行来减少缓存一致性开销。 16 | > 5. **使用内存屏障**:确保内存操作的顺序性。 17 | > 6. **序号栅栏机制**:协调生产者和消费者之间的数据交换进度。 18 | > 19 | > ### 3. 相关概念 20 | > 21 | > - **Disruptor**:核心类,持有 RingBuffer、消费者线程池等。 22 | > - **Ring Buffer**:环形数组,生产者和消费者之间交换数据的桥梁。 23 | > - **Sequencer**:实现并发算法,传递数据。 24 | > - **Sequence**:标识 Ring Buffer 和消费者 Event Processor 的处理进度。 25 | > - **Sequence Barrier**:协调生产者和消费者之间的数据交换进度。 26 | > - **Wait Strategy**:决定消费者如何等待生产者的策略。 27 | > - **Event Processor**:消费者线程,从 Ring Buffer 获取数据。 28 | > - **Event Handler**:实现业务逻辑的 Handler。 29 | > - **Producer**:生产数据的类。 30 | > 31 | > ### 4. 入门 DEMO 32 | > 33 | > - **LongEvent**:定义事件数据。 34 | > - **LongEventFactory**:创建事件实例。 35 | > - **LongEventHandler**:处理事件的逻辑。 36 | > - **LongEventTranslatorOneArg**:将数据翻译到事件中。 37 | > - **LongEventMain**:演示如何使用 Disruptor 框架。 38 | > 39 | > 示例代码展示了如何创建 Disruptor 实例,处理事件,并使用 RingBuffer 发布事件。 40 | 41 | # 初识 Disruptor 框架与广播模式 42 | --------------------------------------------------------------------------------