├── .gitignore ├── README.md └── src ├── chapter1 └── MultiThreadLong.java ├── chapter2 ├── AccountSync.java ├── AccountingVol.java ├── ArrayListMultiThread.java ├── BadLockOnInteger.java ├── CreateThread.java ├── DaemonDemo.java ├── HashMapMultiThread.java ├── InterruptAndStopThread.java ├── InterruptThread.java ├── JoinMain.java ├── PriorityDemo.java ├── SimpleWaitAndNotify.java ├── StopThreadUnsafe.java ├── ThreadGroupName.java └── VolatileDemo.java ├── chapter3 ├── CountDownLatchDemo.java ├── CountTask.java ├── CyclicBarrierDemo.java ├── ExtThreadPool.java ├── FairLock.java ├── IntLock.java ├── LockSupportDemo.java ├── LockSupportIntDemo.java ├── NoTraceDivTaskDemo.java ├── ReadWriteLockDemo.java ├── ReenterLock.java ├── ReenterLockCondition.java ├── RejectThreadPoolDemo.java ├── ScheduledExecutorServiceDemo.java ├── SemapDemo.java ├── ThreadPoolDemo.java ├── TimeLock.java ├── TraceDivTaskDemo.java ├── TraceThreadPoolExecutor.java └── TryLock.java ├── chapter4 ├── AtomicIntegerDemo.java ├── AtomicIntegerFieldUpdaterDemo.java ├── AtomicInterArrayDemo.java ├── AtomicRefrenceDemo.java ├── AtomicStampedReferenceDemo.java ├── DeadLock.java ├── ParseDateDemo.java ├── ParseDateThreadLocalDemo.java ├── ThreadLocalDemo.java └── ThreadLocalDemo_GC.java └── chapter5 ├── Client.java ├── Data.java ├── FutureData.java ├── FutureMain.java ├── LazySingleton.java ├── Main.java ├── Product.java ├── RealData.java ├── RealData2.java ├── Singleton.java ├── StaticSingleton.java └── parallel_compute ├── Div.java ├── Msg.java ├── Multiply.java ├── PStreamMain.java └── Plus.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | ###my own### 4 | out 5 | 6 | ### IntelliJ IDEA ### 7 | .idea 8 | *.iws 9 | *.iml 10 | *.ipr -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 《实战java高并发程序设计》源码整理 2 | 3 | ## 联系作者 4 | 5 | 十三的java学习交流QQ群:```881582471```,```658365129(已满)``` 6 | 7 | ## 相关文章 8 | 9 | [《实战java高并发程序设计》源码整理及读书笔记](http://www.cnblogs.com/han-1034683568/p/6918160.html) 10 | 11 | ## 书籍封面 12 | 13 | ![书籍封面](https://images2018.cnblogs.com/blog/859549/201805/859549-20180509110826184-1374482137.png) 14 | 15 | ## 目录 16 | 17 | 第1章 走入并行世界
18 | 1.1 何去何从的并行计算
19 | 1.1.1 忘掉那该死的并行
20 | 1.1.2 可怕的现实:摩尔定律的失效
21 | 1.1.3 柳暗花明:不断地前进
22 | 1.1.4 光明或是黑暗
23 | 1.2 你必须知道的几个概念
24 | 1.2.1 同步(Synchronous)和异步(Asynchronous)
25 | 1.2.2 并发(Concurrency)和并行(Parallelism)
26 | 1.2.3 临界区
27 | 1.2.4 阻塞(Blocking)和非阻塞(Non-Blocking)
28 | 1.2.5 死锁(Deadlock)、饥饿(Starvation)和活锁(Livelock)
29 | 1.3 并发级别
30 | 1.3.1 阻塞(Blocking)
31 | 1.3.2 无饥饿(Starvation-Free)
32 | 1.3.3 无障碍(Obstruction-Free)
33 | 1.3.4 无锁(Lock-Free)
34 | 1.3.5 无等待(Wait-Free)
35 | 1.4 有关并行的两个重要定律
36 | 1.4.1 Amdahl定律
37 | 1.4.2 Gustafson定律
38 | 1.4.3 Amdahl定律和Gustafson定律是否相互矛盾
39 | 1.5 回到Java:JMM
40 | 1.5.1 原子性(Atomicity)
41 | 1.5.2 可见性(Visibility)
42 | 1.5.3 有序性(Ordering)
43 | 1.5.4 哪些指令不能重排:Happen-Before规则
44 | 1.6 参考文献 45 | 46 | 47 | 第2章 Java并行程序基础
48 | 2.1 有关线程你必须知道的事
49 | 2.2 初始线程:线程的基本操作
50 | 2.2.1 新建线程
51 | 2.2.2 终止线程
52 | 2.2.3 线程中断
53 | 2.2.4 等待(wait)和通知(notify)
54 | 2.2.5 挂起(suspend)和继续执行(resume)线程
55 | 2.2.6 等待线程结束(join)和谦让(yield)
56 | 2.3 volatile与Java内存模型(JMM)
57 | 2.4 分门别类的管理:线程组
58 | 2.5 驻守后台:守护线程(Daemon)
59 | 2.6 先干重要的事:线程优先级
60 | 2.7 线程安全的概念与synchronized
61 | 2.8 程序中的幽灵:隐蔽的错误
62 | 2.8.1 无提示的错误案例
63 | 2.8.2 并发下的ArrayList
64 | 2.8.3 并发下诡异的HashMap
65 | 2.8.4 初学者常见问题:错误的加锁
66 | 2.9 参考文献 67 | 68 | 69 | 第3章 JDK并发包
70 | 3.1 多线程的团队协作:同步控制
71 | 3.1.1 synchronized的功能扩展:重入锁
72 | 3.1.2 重入锁的好搭档:Condition条件
73 | 3.1.3 允许多个线程同时访问:信号量(Semaphore)
74 | 3.1.4 ReadWriteLock读写锁
75 | 3.1.5 倒计时器:CountDownLatch
76 | 3.1.6 循环栅栏:CyclicBarrier
77 | 3.1.7 线程阻塞工具类:LockSupport
78 | 3.2 线程复用:线程池
79 | 3.2.1 什么是线程池
80 | 3.2.2 不要重复发明轮子:JDK对线程池的支持
81 | 3.2.3 刨根究底:核心线程池的内部实现
82 | 3.2.4 超负载了怎么办:拒绝策略
83 | 3.2.5 自定义线程创建:ThreadFactory
84 | 3.2.6 我的应用我做主:扩展线程池
85 | 3.2.7 合理的选择:优化线程池线程数量
86 | 3.2.8 堆栈去哪里了:在线程池中寻找堆栈
87 | 3.2.9 分而治之:Fork/Join框架
88 | 3.3 不要重复发明轮子:JDK的并发容器
89 | 3.3.1 超好用的工具类:并发集合简介
90 | 3.3.2 线程安全的HashMap
91 | 3.3.3 有关List的线程安全
92 | 3.3.4 高效读写的队列:深度剖析ConcurrentLinkedQueue
93 | 3.3.5 高效读取:不变模式下的CopyOnWriteArrayList
94 | 3.3.6 数据共享通道:BlockingQueue
95 | 3.3.7 随机数据结构:跳表(SkipList)
96 | 3.4 参考资料 97 | 98 | 99 | 第4章 锁的优化及注意事项
100 | 4.1 有助于提高"锁"性能的几点建议
101 | 4.1.1 减小锁持有时间
102 | 4.1.2 减小锁粒度
103 | 4.1.3 读写分离锁来替换独占锁
104 | 4.1.4 锁分离
105 | 4.1.5 锁粗化
106 | 4.2 Java虚拟机对锁优化所做的努力
107 | 4.2.1 锁偏向
108 | 4.2.2 轻量级锁
109 | 4.2.3 自旋锁
110 | 4.2.4 锁消除
111 | 4.3 人手一支笔:ThreadLocal
112 | 4.3.1 ThreadLocal的简单使用
113 | 4.3.2 ThreadLocal的实现原理
114 | 4.3.3 对性能有何帮助
115 | 4.4 无锁
116 | 4.4.1 与众不同的并发策略:比较交换(CAS)
117 | 4.4.2 无锁的线程安全整数:AtomicInteger
118 | 4.4.3 Java中的指针:Unsafe类
119 | 4.4.4 无锁的对象引用:AtomicReference
120 | 4.4.5 带有时间戳的对象引用:AtomicStampedReference
121 | 4.4.6 数组也能无锁:AtomicIntegerArray
122 | 4.4.7 让普通变量也享受原子操作:AtomicIntegerFieldUpdater
123 | 4.4.8 挑战无锁算法:无锁的Vector实现
124 | 4.4.9 让线程之间互相帮助:细看SynchronousQueue的实现
125 | 4.5 有关死锁的问题
126 | 4.6 参考文献 127 | 128 | 129 | 第5章 并行模式与算法
130 | 5.1 探讨单例模式
131 | 5.2 不变模式
132 | 5.3 生产者-消费者模式
133 | 5.4 高性能的生产者-消费者:无锁的实现
134 | 5.4.1 无锁的缓存框架:Disruptor
135 | 5.4.2 用Disruptor实现生产者-消费者案例
136 | 5.4.3 提高消费者的响应时间:选择合适的策略
137 | 5.4.4 CPU
Cache的优化:解决伪共享问题
138 | 5.5 Future模式
139 | 5.5.1 Future模式的主要角色
140 | 5.5.2 Future模式的简单实现
141 | 5.5.3 JDK中的Future模式
142 | 5.6 并行流水线
143 | 5.7 并行搜索
144 | 5.8 并行排序
145 | 5.8.1 分离数据相关性:奇偶交换排序
146 | 5.8.2 改进的插入排序:希尔排序
147 | 5.9 并行算法:矩阵乘法
148 | 5.10 准备好了再通知我:网络NIO
149 | 5.10.1 基于Socket的服务端的多线程模式
150 | 5.10.2 使用NIO进行网络编程
151 | 5.10.3 使用NIO来实现客户端
152 | 5.11 读完了再通知我:AIO
153 | 5.11.1 AIO
EchoServer的实现
154 | 5.11.2 AIO
Echo客户端实现
155 | 5.12 参考文献 156 | 157 | 158 | 第6章 Java
8与并发
159 | 6.1 Java
8的函数式编程简介
160 | 6.1.1 函数作为一等公民
161 | 6.1.2 无副作用
162 | 6.1.3 申明式的(Declarative)
163 | 6.1.4 不变的对象
164 | 6.1.5 易于并行
165 | 6.1.6 更少的代码
166 | 6.2 函数式编程基础
167 | 6.2.1 FunctionalInterface注释
168 | 6.2.2 接口默认方法
169 | 6.2.3 lambda表达式
170 | 6.2.4 方法引用
171 | 6.3 一步一步走入函数式编程
172 | 6.4 并行流与并行排序
173 | 6.4.1 使用并行流过滤数据
174 | 6.4.2 从集合得到并行流
175 | 6.4.3 并行排序
176 | 6.5 增强的Future:CompletableFuture
177 | 6.5.1 完成了就通知我
178 | 6.5.2 异步执行任务
179 | 6.5.3 流式调用
180 | 6.5.4 CompletableFuture中的异常处理
181 | 6.5.5 组合多个CompletableFuture
182 | 6.6 读写锁的改进:StampedLock
183 | 6.6.1 StampedLock使用示例
184 | 6.6.2 StampedLock的小陷阱
185 | 6.6.3 有关StampedLock的实现思想
186 | 6.7 原子类的增强
187 | 6.7.1 更快的原子类:LongAdder
188 | 6.7.2 LongAdder的功能增强版:LongAccumulator
189 | 6.8 参考文献 190 | 191 | 192 | 第7章 使用Akka构建高并发程序
193 | 7.1 新并发模型:Actor
194 | 7.2 Akka之Hello
World
195 | 7.3 有关消息投递的一些说明
196 | 7.4 Actor的生命周期
197 | 7.5 监督策略
198 | 7.6 选择Actor
199 | 7.7 消息收件箱(Inbox)
200 | 7.8 消息路由
201 | 7.9 Actor的内置状态转换
202 | 7.10 询问模式:Actor中的Future
203 | 7.11 多个Actor同时修改数据:Agent
204 | 7.12 像数据库一样操作内存数据:软件事务内存
205 | …… 206 | -------------------------------------------------------------------------------- /src/chapter1/MultiThreadLong.java: -------------------------------------------------------------------------------- 1 | package chapter1; 2 | 3 | /** 4 | * Created by 13 on 2017/5/4. 5 | */ 6 | public class MultiThreadLong { 7 | public static long t = 0; 8 | 9 | public static class ChangeT implements Runnable { 10 | /** 11 | * When an object implementing interface Runnable is used 12 | * to create a thread, starting the thread causes the object's 13 | * run method to be called in that separately executing 14 | * thread. 15 | *

16 | * The general contract of the method run is that it may 17 | * take any action whatsoever. 18 | * 19 | * @see Thread#run() 20 | */ 21 | 22 | private long to; 23 | 24 | public ChangeT(long to) { 25 | this.to = to; 26 | } 27 | 28 | @Override 29 | public void run() { 30 | while (true) { 31 | MultiThreadLong.t = to; 32 | Thread.yield(); 33 | } 34 | } 35 | } 36 | 37 | public static class ReadT implements Runnable { 38 | 39 | /** 40 | * When an object implementing interface Runnable is used 41 | * to create a thread, starting the thread causes the object's 42 | * run method to be called in that separately executing 43 | * thread. 44 | *

45 | * The general contract of the method run is that it may 46 | * take any action whatsoever. 47 | * 48 | * @see Thread#run() 49 | */ 50 | @Override 51 | public void run() { 52 | while (true) { 53 | long temp = MultiThreadLong.t; 54 | if (temp != 111L && temp != -999L && temp != 333L && temp != -444L) { 55 | System.out.println(temp); 56 | } 57 | Thread.yield(); 58 | } 59 | } 60 | } 61 | 62 | public static void main(String args[]) { 63 | new Thread(new ChangeT(111L)).start(); 64 | new Thread(new ChangeT(-999L)).start(); 65 | new Thread(new ChangeT(333L)).start(); 66 | new Thread(new ChangeT(-444L)).start(); 67 | new Thread(new ReadT()).start(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/chapter2/AccountSync.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter2/AccountSync.java -------------------------------------------------------------------------------- /src/chapter2/AccountingVol.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter2/AccountingVol.java -------------------------------------------------------------------------------- /src/chapter2/ArrayListMultiThread.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter2/ArrayListMultiThread.java -------------------------------------------------------------------------------- /src/chapter2/BadLockOnInteger.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter2/BadLockOnInteger.java -------------------------------------------------------------------------------- /src/chapter2/CreateThread.java: -------------------------------------------------------------------------------- 1 | package chapter2; 2 | 3 | /** 4 | * Created by 13 on 2017/5/4. 5 | */ 6 | public class CreateThread implements Runnable { 7 | /** 8 | * When an object implementing interface Runnable is used 9 | * to create a thread, starting the thread causes the object's 10 | * run method to be called in that separately executing 11 | * thread. 12 | *

13 | * The general contract of the method run is that it may 14 | * take any action whatsoever. 15 | * 16 | * @see Thread#run() 17 | */ 18 | @Override 19 | public void run() { 20 | System.out.println("Hi!I am Runnable"); 21 | } 22 | 23 | public static void main(String args[]) { 24 | Thread thread = new Thread(new CreateThread()); 25 | thread.start(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/chapter2/DaemonDemo.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter2/DaemonDemo.java -------------------------------------------------------------------------------- /src/chapter2/HashMapMultiThread.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter2/HashMapMultiThread.java -------------------------------------------------------------------------------- /src/chapter2/InterruptAndStopThread.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter2/InterruptAndStopThread.java -------------------------------------------------------------------------------- /src/chapter2/InterruptThread.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter2/InterruptThread.java -------------------------------------------------------------------------------- /src/chapter2/JoinMain.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter2/JoinMain.java -------------------------------------------------------------------------------- /src/chapter2/PriorityDemo.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter2/PriorityDemo.java -------------------------------------------------------------------------------- /src/chapter2/SimpleWaitAndNotify.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter2/SimpleWaitAndNotify.java -------------------------------------------------------------------------------- /src/chapter2/StopThreadUnsafe.java: -------------------------------------------------------------------------------- 1 | package chapter2; 2 | 3 | /** 4 | * Created by 13 on 2017/5/4. 5 | */ 6 | public class StopThreadUnsafe { 7 | 8 | public static User user = new User(); 9 | 10 | public static class User { 11 | private int id; 12 | private String name; 13 | 14 | public int getId() { 15 | return id; 16 | } 17 | 18 | public void setId(int id) { 19 | this.id = id; 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | 30 | public User() { 31 | id = 0; 32 | name = "0"; 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return "User{" + 38 | "name='" + name + '\'' + 39 | ", id=" + id + 40 | '}'; 41 | } 42 | } 43 | 44 | 45 | public static class ChangeObjectThread extends Thread { 46 | public void run() { 47 | while (true) { 48 | synchronized (user) { 49 | int v = (int) (System.currentTimeMillis() / 1000); 50 | user.setId(v); 51 | try { 52 | Thread.sleep(100); 53 | } catch (InterruptedException e) { 54 | e.printStackTrace(); 55 | } 56 | user.setName(v + ""); 57 | } 58 | Thread.yield(); 59 | } 60 | } 61 | } 62 | 63 | public static class ReadObjectThread extends Thread { 64 | public void run() { 65 | while (true) { 66 | synchronized (user) { 67 | if (user.getId() != Integer.parseInt(user.getName())) { 68 | System.out.println(user.toString()); 69 | } 70 | } 71 | Thread.yield(); 72 | } 73 | } 74 | } 75 | 76 | 77 | public static void main(String args[]) throws InterruptedException { 78 | new ReadObjectThread().start(); 79 | while (true) { 80 | Thread thread = new ChangeObjectThread(); 81 | thread.start(); 82 | Thread.sleep(150); 83 | thread.stop(); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/chapter2/ThreadGroupName.java: -------------------------------------------------------------------------------- 1 | package chapter2; 2 | 3 | /** 4 | * Created by 13 on 2017/5/4. 5 | */ 6 | public class ThreadGroupName implements Runnable { 7 | /** 8 | * When an object implementing interface Runnable is used 9 | * to create a thread, starting the thread causes the object's 10 | * run method to be called in that separately executing 11 | * thread. 12 | *

13 | * The general contract of the method run is that it may 14 | * take any action whatsoever. 15 | * 16 | * @see Thread#run() 17 | */ 18 | @Override 19 | public void run() { 20 | String groupAndName = Thread.currentThread().getThreadGroup().getName() + "-" + Thread.currentThread().getName(); 21 | while (true) { 22 | System.out.println("I am " + groupAndName); 23 | try { 24 | Thread.sleep(3000); 25 | } catch (InterruptedException e) { 26 | e.printStackTrace(); 27 | } 28 | } 29 | } 30 | 31 | public static void main(String args[]) { 32 | ThreadGroup tg = new ThreadGroup("PrintGroup"); 33 | Thread t1 = new Thread(tg, new ThreadGroupName(), "T1"); 34 | Thread t2 = new Thread(tg, new ThreadGroupName(), "T2"); 35 | t1.start(); 36 | t2.start(); 37 | System.out.println(tg.activeCount()); 38 | Thread t3 = new Thread(tg, new ThreadGroupName(), "T3"); 39 | t3.start(); 40 | System.out.println(tg.activeCount()); 41 | tg.list(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/chapter2/VolatileDemo.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter2/VolatileDemo.java -------------------------------------------------------------------------------- /src/chapter3/CountDownLatchDemo.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter3/CountDownLatchDemo.java -------------------------------------------------------------------------------- /src/chapter3/CountTask.java: -------------------------------------------------------------------------------- 1 | package chapter3; 2 | 3 | import java.util.ArrayList; 4 | import java.util.concurrent.ExecutionException; 5 | import java.util.concurrent.ForkJoinPool; 6 | import java.util.concurrent.ForkJoinTask; 7 | import java.util.concurrent.RecursiveTask; 8 | 9 | /** 10 | * Created by 13 on 2017/5/5. 11 | */ 12 | public class CountTask extends RecursiveTask { 13 | private static final int THRESHOLD = 10000; 14 | 15 | private long start; 16 | private long end; 17 | 18 | public CountTask(long start, long end) { 19 | this.start = start; 20 | this.end = end; 21 | } 22 | 23 | 24 | @Override 25 | protected Long compute() { 26 | long sum = 0; 27 | boolean canCompute = (end - start) < THRESHOLD; 28 | if (canCompute) { 29 | for (long i = start; i <= end; i++) { 30 | sum += i; 31 | } 32 | } else { 33 | long step = (start + end) / 100; 34 | 35 | ArrayList subTasks = new ArrayList(); 36 | long pos = start; 37 | 38 | for (int i = 0; i < 100; i++) { 39 | long lastOne = pos + step; 40 | if (lastOne > end) { 41 | lastOne = end; 42 | } 43 | CountTask subTask = new CountTask(pos, lastOne); 44 | pos += step + 1; 45 | subTasks.add(subTask); 46 | subTask.fork(); 47 | } 48 | 49 | for (CountTask t : subTasks) { 50 | sum += (Long) t.join(); 51 | } 52 | } 53 | 54 | 55 | return sum; 56 | } 57 | 58 | 59 | public static void main(String args[]) { 60 | ForkJoinPool forkJoinPool = new ForkJoinPool(); 61 | CountTask task = new CountTask(0, 200000L); 62 | ForkJoinTask result = forkJoinPool.submit(task); 63 | 64 | long res = 0; 65 | try { 66 | res = result.get(); 67 | System.out.println("sum=" + res); 68 | } catch (InterruptedException e) { 69 | e.printStackTrace(); 70 | } catch (ExecutionException e) { 71 | e.printStackTrace(); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/chapter3/CyclicBarrierDemo.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter3/CyclicBarrierDemo.java -------------------------------------------------------------------------------- /src/chapter3/ExtThreadPool.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter3/ExtThreadPool.java -------------------------------------------------------------------------------- /src/chapter3/FairLock.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter3/FairLock.java -------------------------------------------------------------------------------- /src/chapter3/IntLock.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter3/IntLock.java -------------------------------------------------------------------------------- /src/chapter3/LockSupportDemo.java: -------------------------------------------------------------------------------- 1 | package chapter3; 2 | 3 | import java.util.concurrent.locks.LockSupport; 4 | 5 | /** 6 | * Created by 13 on 2017/5/5. 7 | */ 8 | public class LockSupportDemo { 9 | public static Object u = new Object(); 10 | static ChangeObjectThread t1 = new ChangeObjectThread("t1"); 11 | static ChangeObjectThread t2 = new ChangeObjectThread("t2"); 12 | 13 | public static class ChangeObjectThread extends Thread { 14 | public ChangeObjectThread(String name) { 15 | super.setName(name); 16 | } 17 | 18 | public void run() { 19 | synchronized (u) { 20 | System.out.println("in " + getName()); 21 | LockSupport.park(); 22 | } 23 | } 24 | } 25 | 26 | 27 | public static void main(String args[]) throws InterruptedException { 28 | t1.start(); 29 | Thread.sleep(100); 30 | t2.start(); 31 | LockSupport.unpark(t1); 32 | LockSupport.unpark(t2); 33 | t1.join(); 34 | t2.join(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/chapter3/LockSupportIntDemo.java: -------------------------------------------------------------------------------- 1 | package chapter3; 2 | 3 | import java.util.concurrent.locks.LockSupport; 4 | 5 | /** 6 | * Created by 13 on 2017/5/5. 7 | */ 8 | public class LockSupportIntDemo { 9 | public static Object u = new Object(); 10 | static ChangeObjectThread t1 = new ChangeObjectThread("t1"); 11 | static ChangeObjectThread t2 = new ChangeObjectThread("t2"); 12 | 13 | public static class ChangeObjectThread extends Thread { 14 | public ChangeObjectThread(String name) { 15 | super.setName(name); 16 | } 17 | 18 | public void run() { 19 | synchronized (u) { 20 | System.out.println("in " + getName()); 21 | LockSupport.park(); 22 | if (Thread.interrupted()) { 23 | } 24 | System.out.println(getName() + "被中断"); 25 | } 26 | System.out.println(getName() + "继续执行"); 27 | } 28 | } 29 | 30 | 31 | public static void main(String args[]) throws InterruptedException { 32 | t1.start(); 33 | Thread.sleep(100); 34 | t2.start(); 35 | LockSupport.unpark(t1); 36 | LockSupport.unpark(t2); 37 | t1.join(); 38 | t2.join(); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/chapter3/NoTraceDivTaskDemo.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter3/NoTraceDivTaskDemo.java -------------------------------------------------------------------------------- /src/chapter3/ReadWriteLockDemo.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter3/ReadWriteLockDemo.java -------------------------------------------------------------------------------- /src/chapter3/ReenterLock.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter3/ReenterLock.java -------------------------------------------------------------------------------- /src/chapter3/ReenterLockCondition.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter3/ReenterLockCondition.java -------------------------------------------------------------------------------- /src/chapter3/RejectThreadPoolDemo.java: -------------------------------------------------------------------------------- 1 | package chapter3; 2 | 3 | import java.util.concurrent.*; 4 | 5 | /** 6 | * Created by 13 on 2017/5/5. 7 | */ 8 | public class RejectThreadPoolDemo { 9 | public static class MyTask implements Runnable { 10 | 11 | @Override 12 | public void run() { 13 | System.out.println(System.currentTimeMillis() + ":Thread ID:" + Thread.currentThread().getId()); 14 | try { 15 | Thread.sleep(100); 16 | } catch (InterruptedException e) { 17 | e.printStackTrace(); 18 | } 19 | } 20 | } 21 | 22 | 23 | public static void main(String args[]) throws InterruptedException { 24 | MyTask myTask = new MyTask(); 25 | 26 | ExecutorService executorService = new ThreadPoolExecutor(5, 5, 0L, TimeUnit.SECONDS, new LinkedBlockingDeque(10), Executors.defaultThreadFactory() 27 | , new RejectedExecutionHandler() { 28 | @Override 29 | public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { 30 | System.out.println(r.toString() + " is discard"); 31 | } 32 | }); 33 | 34 | for (int i = 0; i < 100; i++) { 35 | executorService.submit(myTask); 36 | Thread.sleep(10); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/chapter3/ScheduledExecutorServiceDemo.java: -------------------------------------------------------------------------------- 1 | package chapter3; 2 | 3 | import java.util.concurrent.Executors; 4 | import java.util.concurrent.ScheduledExecutorService; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | /** 8 | * Created by 13 on 2017/5/5. 9 | */ 10 | public class ScheduledExecutorServiceDemo { 11 | public static void main(String args[]) { 12 | ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10); 13 | 14 | scheduledExecutorService.scheduleAtFixedRate(new Runnable() { 15 | @Override 16 | public void run() { 17 | try { 18 | Thread.sleep(1000); 19 | System.out.println(System.currentTimeMillis() / 1000); 20 | } catch (InterruptedException e) { 21 | e.printStackTrace(); 22 | } 23 | } 24 | }, 0, 2, TimeUnit.SECONDS); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/chapter3/SemapDemo.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter3/SemapDemo.java -------------------------------------------------------------------------------- /src/chapter3/ThreadPoolDemo.java: -------------------------------------------------------------------------------- 1 | package chapter3; 2 | 3 | import java.util.concurrent.ExecutorService; 4 | import java.util.concurrent.Executors; 5 | 6 | /** 7 | * Created by 13 on 2017/5/5. 8 | */ 9 | public class ThreadPoolDemo { 10 | public static class MyTask implements Runnable { 11 | 12 | @Override 13 | public void run() { 14 | System.out.println(System.currentTimeMillis() + "Thread ID:" + Thread.currentThread().getId()); 15 | 16 | try { 17 | Thread.sleep(1000); 18 | } catch (InterruptedException e) { 19 | e.printStackTrace(); 20 | } 21 | } 22 | } 23 | 24 | 25 | public static void main(String args[]) { 26 | MyTask myTask = new MyTask(); 27 | ExecutorService executorService = Executors.newFixedThreadPool(5); 28 | for (int i = 0; i < 20; i++) { 29 | executorService.submit(myTask); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/chapter3/TimeLock.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter3/TimeLock.java -------------------------------------------------------------------------------- /src/chapter3/TraceDivTaskDemo.java: -------------------------------------------------------------------------------- 1 | package chapter3; 2 | 3 | import java.util.concurrent.SynchronousQueue; 4 | import java.util.concurrent.ThreadPoolExecutor; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | /** 8 | * Created by 13 on 2017/5/5. 9 | */ 10 | public class TraceDivTaskDemo { 11 | public static void main(String args[]) { 12 | ThreadPoolExecutor threadPoolExecutor = new TraceThreadPoolExecutor(0, Integer.MAX_VALUE, 0L, TimeUnit.SECONDS, new SynchronousQueue()); 13 | for (int i = 0; i < 5; i++) { 14 | threadPoolExecutor.execute(new NoTraceDivTaskDemo.DivTask(100, i)); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/chapter3/TraceThreadPoolExecutor.java: -------------------------------------------------------------------------------- 1 | package chapter3; 2 | 3 | import java.util.concurrent.BlockingQueue; 4 | import java.util.concurrent.ThreadPoolExecutor; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | /** 8 | * Created by 13 on 2017/5/5. 9 | */ 10 | public class TraceThreadPoolExecutor extends ThreadPoolExecutor { 11 | 12 | public TraceThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue) { 13 | super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); 14 | } 15 | 16 | public void execute(Runnable task) { 17 | super.execute(wrap(task, clientTrace(), Thread.currentThread().getName())); 18 | } 19 | 20 | private Runnable wrap(final Runnable task, final Exception clientTrace, String name) { 21 | return new Runnable() { 22 | @Override 23 | public void run() { 24 | try { 25 | task.run(); 26 | } catch (Exception e) { 27 | clientTrace.printStackTrace(); 28 | throw e; 29 | } 30 | } 31 | }; 32 | } 33 | 34 | private Exception clientTrace() { 35 | return new Exception("Client stack trace"); 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /src/chapter3/TryLock.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter3/TryLock.java -------------------------------------------------------------------------------- /src/chapter4/AtomicIntegerDemo.java: -------------------------------------------------------------------------------- 1 | package chapter4; 2 | 3 | import java.util.concurrent.atomic.AtomicInteger; 4 | 5 | 6 | public class AtomicIntegerDemo { 7 | static AtomicInteger i = new AtomicInteger(); 8 | 9 | public static class AddThread implements Runnable { 10 | 11 | @Override 12 | public void run() { 13 | for (int j = 0; j < 10002; j++) { 14 | i.incrementAndGet(); 15 | } 16 | } 17 | } 18 | 19 | public static void main(String args[]) throws InterruptedException { 20 | 21 | Thread[] threads = new Thread[10]; 22 | 23 | for (int j = 0; j < 10; j++) { 24 | threads[j] = new Thread(new AddThread()); 25 | } 26 | 27 | for (int j = 0; j < 10; j++) { 28 | threads[j].start(); 29 | } 30 | for (int j = 0; j < 10; j++) { 31 | threads[j].join(); 32 | } 33 | 34 | System.out.println(i); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/chapter4/AtomicIntegerFieldUpdaterDemo.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter4/AtomicIntegerFieldUpdaterDemo.java -------------------------------------------------------------------------------- /src/chapter4/AtomicInterArrayDemo.java: -------------------------------------------------------------------------------- 1 | package chapter4; 2 | 3 | import java.util.concurrent.atomic.AtomicIntegerArray; 4 | 5 | /** 6 | * Created by 13 on 2017/5/6. 7 | */ 8 | public class AtomicInterArrayDemo { 9 | static AtomicIntegerArray array = new AtomicIntegerArray(10); 10 | 11 | public static class AddThread implements Runnable { 12 | @Override 13 | public void run() { 14 | for (int i = 0; i < 10000; i++) { 15 | array.getAndIncrement(i % array.length()); 16 | } 17 | } 18 | } 19 | 20 | public static void main(String args[]) throws InterruptedException { 21 | Thread[] threads = new Thread[10]; 22 | for (int i = 0; i < 10; i++) { 23 | threads[i] = new Thread(new AddThread()); 24 | } 25 | 26 | for (int i = 0; i < 10; i++) { 27 | threads[i].start(); 28 | } 29 | for (int i = 0; i < 10; i++) { 30 | threads[i].join(); 31 | System.out.println(array); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/chapter4/AtomicRefrenceDemo.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter4/AtomicRefrenceDemo.java -------------------------------------------------------------------------------- /src/chapter4/AtomicStampedReferenceDemo.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter4/AtomicStampedReferenceDemo.java -------------------------------------------------------------------------------- /src/chapter4/DeadLock.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter4/DeadLock.java -------------------------------------------------------------------------------- /src/chapter4/ParseDateDemo.java: -------------------------------------------------------------------------------- 1 | package chapter4; 2 | 3 | import java.text.ParseException; 4 | import java.text.SimpleDateFormat; 5 | import java.util.Date; 6 | import java.util.concurrent.ExecutorService; 7 | import java.util.concurrent.Executors; 8 | 9 | /** 10 | * Created by 13 on 2017/5/6. 11 | */ 12 | public class ParseDateDemo { 13 | private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 14 | 15 | public static class ParseDate implements Runnable { 16 | int i = 0; 17 | 18 | public ParseDate(int i) { 19 | this.i = i; 20 | } 21 | 22 | @Override 23 | public void run() { 24 | try { 25 | Date date = sdf.parse("2017-05-06 12:33:" + i % 60); 26 | System.out.println(i + ":" + date); 27 | } catch (ParseException e) { 28 | e.printStackTrace(); 29 | } 30 | } 31 | } 32 | 33 | public static void main(String args[]) { 34 | ExecutorService executorService = Executors.newFixedThreadPool(10); 35 | for (int i = 0; i < 1000; i++) { 36 | executorService.execute(new ParseDate(i)); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/chapter4/ParseDateThreadLocalDemo.java: -------------------------------------------------------------------------------- 1 | package chapter4; 2 | 3 | import java.text.ParseException; 4 | import java.text.SimpleDateFormat; 5 | import java.util.Date; 6 | import java.util.concurrent.ExecutorService; 7 | import java.util.concurrent.Executors; 8 | 9 | /** 10 | * Created by 13 on 2017/5/6. 11 | */ 12 | public class ParseDateThreadLocalDemo { 13 | private static ThreadLocal threadLocal = new ThreadLocal(); 14 | 15 | public static class ParseDate implements Runnable { 16 | int i = 0; 17 | 18 | public ParseDate(int i) { 19 | this.i = i; 20 | } 21 | 22 | @Override 23 | public void run() { 24 | try { 25 | if (threadLocal.get() == null) { 26 | threadLocal.set(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); 27 | } 28 | Date date = threadLocal.get().parse("2017-05-06 12:33:" + i % 60); 29 | System.out.println(i + ":" + date); 30 | } catch (ParseException e) { 31 | e.printStackTrace(); 32 | } 33 | } 34 | } 35 | 36 | public static void main(String args[]) { 37 | ExecutorService executorService = Executors.newFixedThreadPool(10); 38 | for (int i = 0; i < 1000; i++) { 39 | executorService.execute(new ParseDate(i)); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/chapter4/ThreadLocalDemo.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter4/ThreadLocalDemo.java -------------------------------------------------------------------------------- /src/chapter4/ThreadLocalDemo_GC.java: -------------------------------------------------------------------------------- 1 | package chapter4; 2 | 3 | import java.text.ParseException; 4 | import java.text.SimpleDateFormat; 5 | import java.util.Date; 6 | import java.util.concurrent.CountDownLatch; 7 | import java.util.concurrent.ExecutorService; 8 | import java.util.concurrent.Executors; 9 | 10 | /** 11 | * Created by 13 on 2017/5/6. 12 | */ 13 | public class ThreadLocalDemo_GC { 14 | static volatile ThreadLocal threadLocal = new ThreadLocal() { 15 | protected void finalize() throws Throwable { 16 | System.out.println(this.toString() + " is gc"); 17 | } 18 | }; 19 | 20 | static volatile CountDownLatch countDownLatch = new CountDownLatch(100); 21 | 22 | public static class ParseDate implements Runnable { 23 | int i = 0; 24 | 25 | public ParseDate(int i) { 26 | this.i = i; 27 | } 28 | 29 | @Override 30 | public void run() { 31 | 32 | try { 33 | if (threadLocal.get() == null) { 34 | threadLocal.set(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") { 35 | protected void finalize() throws Throwable { 36 | System.out.println(this.toString() + " is gc"); 37 | } 38 | }); 39 | System.out.println(Thread.currentThread().getId() + " create SimpleDatFormat"); 40 | } 41 | Date date = threadLocal.get().parse("2017-05-06 12:33:" + i % 60); 42 | System.out.println(i + ":" + date); 43 | } catch (ParseException e) { 44 | e.printStackTrace(); 45 | } finally { 46 | countDownLatch.countDown(); 47 | } 48 | 49 | } 50 | } 51 | 52 | 53 | public static void main(String args[]) throws InterruptedException { 54 | ExecutorService executorService = Executors.newFixedThreadPool(10); 55 | for (int i = 0; i < 100; i++) { 56 | executorService.execute(new ParseDate(i)); 57 | } 58 | countDownLatch.await(); 59 | 60 | System.out.println("mission complete!"); 61 | 62 | threadLocal = null; 63 | System.gc(); 64 | System.out.println("first GC complete!!"); 65 | 66 | threadLocal = new ThreadLocal(); 67 | 68 | countDownLatch = new CountDownLatch(100); 69 | 70 | for (int i = 0; i < 100; i++) { 71 | executorService.execute(new ParseDate(i)); 72 | } 73 | 74 | countDownLatch.await(); 75 | Thread.sleep(1000); 76 | System.gc(); 77 | System.out.println("second GC complete!"); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/chapter5/Client.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter5/Client.java -------------------------------------------------------------------------------- /src/chapter5/Data.java: -------------------------------------------------------------------------------- 1 | package chapter5; 2 | 3 | /** 4 | * Created by 13 on 2017/5/8. 5 | */ 6 | public interface Data { 7 | public String getResult(); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/chapter5/FutureData.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter5/FutureData.java -------------------------------------------------------------------------------- /src/chapter5/FutureMain.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter5/FutureMain.java -------------------------------------------------------------------------------- /src/chapter5/LazySingleton.java: -------------------------------------------------------------------------------- 1 | package chapter5; 2 | 3 | /** 4 | * Created by 13 on 2017/5/6. 5 | */ 6 | public class LazySingleton { 7 | private LazySingleton() { 8 | System.out.println("LazySingleton is create"); 9 | } 10 | 11 | private static LazySingleton instance = null; 12 | 13 | public static synchronized LazySingleton getInstance() { 14 | if (instance == null) { 15 | instance = new LazySingleton(); 16 | } 17 | return instance; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/chapter5/Main.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter5/Main.java -------------------------------------------------------------------------------- /src/chapter5/Product.java: -------------------------------------------------------------------------------- 1 | package chapter5; 2 | 3 | /** 4 | * Created by 13 on 2017/5/6. 5 | */ 6 | public final class Product { 7 | private final String no; 8 | private final String name; 9 | private final String price; 10 | 11 | public Product(String no, String name, String price) { 12 | super(); 13 | this.no = no; 14 | this.name = name; 15 | this.price = price; 16 | } 17 | 18 | public String getNo() { 19 | return no; 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | public String getPrice() { 27 | return price; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/chapter5/RealData.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZHENFENG13/concurrent-programming/0744c27d5a30f7638b820ac570a2fea31ddfb934/src/chapter5/RealData.java -------------------------------------------------------------------------------- /src/chapter5/RealData2.java: -------------------------------------------------------------------------------- 1 | package chapter5; 2 | 3 | import java.util.concurrent.Callable; 4 | 5 | /** 6 | * Created by 13 on 2017/5/8. 7 | */ 8 | public class RealData2 implements Callable { 9 | private String data; 10 | 11 | public RealData2(String data) { 12 | this.data = data; 13 | } 14 | 15 | @Override 16 | public String call() throws Exception { 17 | StringBuffer stringBuffer = new StringBuffer(); 18 | for (int i = 0; i < 10; i++) { 19 | stringBuffer.append(data); 20 | Thread.sleep(100); 21 | } 22 | 23 | return stringBuffer.toString(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter5/Singleton.java: -------------------------------------------------------------------------------- 1 | package chapter5; 2 | 3 | /** 4 | * Created by 13 on 2017/5/6. 5 | */ 6 | public class Singleton { 7 | private Singleton() { 8 | System.out.println("Singleton is create"); 9 | } 10 | 11 | private static Singleton instance = new Singleton(); 12 | 13 | public static Singleton getInstance() { 14 | return instance; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/chapter5/StaticSingleton.java: -------------------------------------------------------------------------------- 1 | package chapter5; 2 | 3 | /** 4 | * Created by 13 on 2017/5/6. 5 | */ 6 | public class StaticSingleton { 7 | private StaticSingleton() { 8 | System.out.println("StaticSingle is create"); 9 | } 10 | 11 | private static class SingletonHolder { 12 | private static StaticSingleton instance = new StaticSingleton(); 13 | } 14 | 15 | public static StaticSingleton getInstance() { 16 | return SingletonHolder.instance; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/chapter5/parallel_compute/Div.java: -------------------------------------------------------------------------------- 1 | package chapter5.parallel_compute; 2 | 3 | import java.util.concurrent.BlockingDeque; 4 | import java.util.concurrent.LinkedBlockingDeque; 5 | 6 | /** 7 | * Created by 13 on 2017/5/9. 8 | */ 9 | public class Div implements Runnable { 10 | public static BlockingDeque blockingDeque = new LinkedBlockingDeque(); 11 | 12 | @Override 13 | public void run() { 14 | while (true) { 15 | Msg msg = null; 16 | try { 17 | msg = blockingDeque.take(); 18 | msg.i = msg.i / 2; 19 | System.out.println(msg.orgStr + "=" + msg.i); 20 | } catch (InterruptedException e) { 21 | e.printStackTrace(); 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter5/parallel_compute/Msg.java: -------------------------------------------------------------------------------- 1 | package chapter5.parallel_compute; 2 | 3 | /** 4 | * Created by 13 on 2017/5/9. 5 | */ 6 | public class Msg { 7 | 8 | public double i; 9 | public double j; 10 | public String orgStr = null; 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/chapter5/parallel_compute/Multiply.java: -------------------------------------------------------------------------------- 1 | package chapter5.parallel_compute; 2 | 3 | import java.util.concurrent.BlockingDeque; 4 | import java.util.concurrent.LinkedBlockingDeque; 5 | 6 | /** 7 | * Created by 13 on 2017/5/9. 8 | */ 9 | public class Multiply implements Runnable { 10 | public static BlockingDeque blockingDeque = new LinkedBlockingDeque(); 11 | 12 | @Override 13 | public void run() { 14 | while (true) { 15 | Msg msg = null; 16 | try { 17 | msg = blockingDeque.take(); 18 | msg.i = msg.j * msg.i; 19 | Div.blockingDeque.add(msg); 20 | } catch (InterruptedException e) { 21 | e.printStackTrace(); 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter5/parallel_compute/PStreamMain.java: -------------------------------------------------------------------------------- 1 | package chapter5.parallel_compute; 2 | 3 | /** 4 | * Created by 13 on 2017/5/9. 5 | */ 6 | public class PStreamMain { 7 | public static void main(String args[]) { 8 | 9 | new Thread(new Plus()).start(); 10 | new Thread(new Multiply()).start(); 11 | new Thread(new Div()).start(); 12 | 13 | 14 | for (int i = 0; i <= 1000; i++) { 15 | for (int j = 0; j <= 1000; j++) { 16 | Msg msg = new Msg(); 17 | msg.i = i; 18 | msg.j = j; 19 | msg.orgStr = "((" + i + "+" + j + ")*" + i + ")/2"; 20 | Plus.blockingDeque.add(msg); 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/chapter5/parallel_compute/Plus.java: -------------------------------------------------------------------------------- 1 | package chapter5.parallel_compute; 2 | 3 | import java.util.concurrent.BlockingDeque; 4 | import java.util.concurrent.LinkedBlockingDeque; 5 | 6 | /** 7 | * Created by 13 on 2017/5/9. 8 | */ 9 | public class Plus implements Runnable { 10 | 11 | public static BlockingDeque blockingDeque = new LinkedBlockingDeque(); 12 | 13 | @Override 14 | public void run() { 15 | while (true) { 16 | Msg msg = null; 17 | try { 18 | msg = blockingDeque.take(); 19 | msg.i = msg.j + msg.i; 20 | Multiply.blockingDeque.add(msg); 21 | } catch (InterruptedException e) { 22 | e.printStackTrace(); 23 | } 24 | } 25 | } 26 | } 27 | --------------------------------------------------------------------------------