Disruptor-伪共享

Disruptor之所以快的原因之一是因为考虑内存的伪共享问题
伪共享测试地址:http://mechanical-sympathy.blogspot.com/2011/07/false-sharing.html
根据文章内容述说的内容是内存中有三级缓存,越接近cpu的缓存越快也越小,当两个线程同时更新数据,刚好两个数据都在同一个缓存行,更新一个数据,会引起读取另一个数据失效,会使数据来回获取保持更新,来回会耗时较大,所以使用填充沾满一个缓存行,使更新没那么频繁。

Disruptor怎么使用的

class LhsPadding
{
    protected long p1, p2, p3, p4, p5, p6, p7;
}

class Value extends LhsPadding
{
    protected volatile long value;
}

class RhsPadding extends Value
{
    protected long p9, p10, p11, p12, p13, p14, p15;
}

/**
 * <p>Concurrent sequence class used for tracking the progress of
 * the ring buffer and event processors.  Support a number
 * of concurrent operations including CAS and order writes.
 *
 * <p>Also attempts to be more efficient with regards to false
 * sharing by adding padding around the volatile field.
 */
public class Sequence extends RhsPadding
{
    static final long INITIAL_VALUE = -1L;
    private static final Unsafe UNSAFE;
    private static final long VALUE_OFFSET;

伪共享填充.png

左右填充的目的是为了使真是的数据正好处在一个缓存行内,而不受别的数据影响

应用场景
Log4J2

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容