12. 第二步:lazySet
Java并发编程优化之阻塞队列
Data eXchange Platform| zhouchen.zm
• AtomicXXX.lazySet()保证StoreStore
• 但不保证StoreLoad
• 保证最终一致性
• 一个轻量的volatile
• Unsafe.putOrderedXXX
• Ops约1700万
"This is a niche method that is sometimes useful
when fine-tuning code using non-blocking data
structures. The semantics are that the write is
guaranteed not to be re-ordered with any previous
write, but may be reordered with subsequent
operations(or equivalently, might not be visible to
other threads) until some other volatile write or
synchronizing action occurs).“
--Doug Lea
13. 第三步:求模优化
Java并发编程优化之阻塞队列
Data eXchange Platform| zhouchen.zm
• & (k pow 2) - 1 替代%
• Ops约2200万
public boolean offer(final E e) {
…
buffer[(int) (currentTail % buffer.length)] = e;
…
}
public boolean offer(final E e) {
…
buffer[(int) currentTail & mask] = e;
…
}
15. 第四步:去除伪共享
Java并发编程优化之阻塞队列
Data eXchange Platform| zhouchen.zm
• Ops约4000万
public class PaddedAtomicLong extends AtomicLong {
private static final long serialVersionUID = 1L;
public PaddedAtomicLong() {
}
public PaddedAtomicLong(final long initialValue) {
super(initialValue);
}
public long p1, p2, p3, p4, p5, p6;
}
25. 推荐读物
Java并发编程优化之阻塞队列
Data eXchange Platform| zhouchen.zm
• What every programmer should know about memory
• Intel® 64 and IA-32 Architectures Software Developer Manuals
• The Art of Multiprocessor Programming
• The JSR-133 Cookbook for Compiler Writers (Java Memory Model)
• 本人博客: http://coderplay.javaeye.com
26. Q & A
Data eXchange Platform| zhouchen.zm
Java并发编程优化之阻塞队列
作者:周忱 | 数据平台-DXP
微博:@MinZhou
邮箱:zhouchen.zm@taobao.com