李祥乾
技术工程部
double pi = 3.1415926D; //1
double r = 2.3D; //2
double area = pi * r * r; //3
1
2
3
数据依赖
class ReorderExample {
int a = 0;
boolean flag = false;
public void writer() {
a = 3; //1
flag = true; //2
}
Public void reader() {
if (flag) { //3
int i = a * a; //4
……
}
}
}
flag = true; //2
if (flag) //3
int i = a * a; //4
a = 3; //1
重排序
thread 1
thread 2
i = 0;
设置为volatile
假设每个线程都拥有一个虚拟的CPU
local memory
a |
---|
flag |
CPU1
a |
---|
flag |
shared MEM
bus
local memory
a |
---|
flag |
CPU2
bus
volatile写(1)-写register
volatile写(2)-写回共享内存
volatile读(1)-register置为无效
volatile读(2)-从共享内存读
普通写-写register
普通读-读register
volatile写
普通写
内存屏障
普通读
volatile读
volatile写
volatile读
class ReorderExample {
int a = 0;
volatile boolean flag = false;
public void writer() {
a = 3; //1
flag = true; //2
}
Public void reader() {
if (flag) { //3
int i = a * a; //4
……
}
}
}
flag = true; //2
if (flag) //3
int i = a * a; //4
thread 1
thread 2
i = 9;
a = 3; //1
public FinalExample {
int i;
final int j;
static FinalExample INSTANCE;
public FinalExample(int i, int j) {
this.i = i; //2 写普通域
this.j = j; //3 写final域
}
public static void init() {
INSTANCE = new FinalExample(2, 3); //1
}
public static void read() {
if (INSTANCE != null) {
int readI = INSTANCE.i; //4 读普通域
int readJ = INSTANCE.j; //5 读final域
}
}
}
进入FinalExample构造方法
thread 1
thread 2
this.j = j;//3 写final域
this.i = i;//2 写普通域
构造完成返回给INSTANCE引用
if (INSTANCE != null)
int readI = INSTANCE.i;
int readJ = INSTANCE.j;
重排序,逃逸
readI=0;readJ=3;
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
...
}
public class Executors {
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(
1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
}
Executors工厂类的工厂方法
SynchronousQueue - 大小恒定为1的BlockingQueue,放一个必须取一个
class MergeSort<T> {
private List<T> merge(List<T> list0, List<T> list1) {
//...
}
private Entry<List<T>, List<T>> split(List<T> list) {
//...
}
public mergeSort(List<T> list) {
Preconditions.checkArgument(list != null && list.size() > 0);
if (list.size() == 1) {
return list;
}
Entry<List<T>, List<T>> splitted = split(list);
List<T> merge0 = mergeSort(splitted.getKey());
List<T> merge1 = mergeSort(splitted.getValue());
return merge(merge0, merge1);
}
}
单线程version
class MergeSortTask<T> extends RecursiveTask<List<T>> {
private List<T> merge(List<T> list0, List<T> list1) {
//...functional implementation
}
private Entry<List<T>, List<T>> split(List<T> list) {
//...functional implementation
}
protected final List<T> list;
public MergeSortTask(List<T> list) { this.list = list; }
@Override
protected List<T> compute() {
if (list.size() == 1) { return list; }
Entry<List<T>, List<T>> splits = split(list);
ForkJoinTask<List<T>> task0 = new MergeSortTask<T>(splits.getKey()).fork();
ForkJoinTask<List<T>> task1 = new MergeSortTask<T>(splits.getValue()).fork();
List<T> list0 = task0.join();
List<T> list1 = task1.join();
return merge(list0, list1);
}
}
ForkJoinTask parrallel version
把它丢进ForkJoinPool就得了
public class SortUtils {
public <T> Future<List<T>> mergeSort(List<T> list) {
ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime()
.availableProcessors());
return pool.submit(new MergeSortTask<T>(list));
}
}
Worker
Worker
Worker
...
ForkJoinTask 0 |
---|
ForkJoinTask 1 |
ForkJoinTask 2 |
ForkJoinTask 0 |
---|
ForkJoinTask 1 |
ForkJoinTask 2 |
ForkJoinTask 3 |
ForkJoinTask 4 |
ForkJoinTask stealed |
---|
run out of tasks
steals
forks
Q&A