当前位置:网站首页>Disruptor(一)Sequence
Disruptor(一)Sequence
2022-06-26 00:14:00 【扶朕去网吧】
在大扎了解Disruptor的过程中,根据各个组件的的依赖程度,分别按照以下顺序来分篇说明每个组件的作用和用法,在下面的过程中,会以源码为主,手册为辅的方式,描述各个组件的作用以及原因。
Sequence -> Sequencer-> Sequence Barrier -> Event -> Producer -> Ring Buffer -> Event Processor -> Wait Strategy -> Event Handler
1. 前言
并发sequence是用来进行ring buffer与event处理者情况进行跟踪的,支持大量CAS和顺序写的并发操作。
并且通过对volatile字段周围填充的方式对伪共享问题更有效果,
2. 方法(一致性保证)
使用了一个VarHandle句柄来保证访问变量数据的并发操作,涉及到了sequence的3个方法:
compareAndSet
调用了VarHandle的compareAndSet方法进行操作。
/**
* Perform a compare and set operation on the sequence.
*
* @param expectedValue The expected current value.
* @param newValue The value to update to.
* @return true if the operation succeeds, false otherwise.
*/
public boolean compareAndSet(final long expectedValue, final long newValue)
{
return VALUE_FIELD.compareAndSet(this, expectedValue, newValue);
}incrementAndGet与addAndGet
实际调用了VarHandle的getAndAdd方法进行操作,这里sequence的方法是add->get,而VarHandle的方法是get->add,所以在这个方法里面把调用VarHandler的值又加了个递增值。
/**
* Atomically add the supplied value.
*
* @param increment The value to add to the sequence.
* @return The value after the increment.
*/
public long addAndGet(final long increment)
{
return (long) VALUE_FIELD.getAndAdd(this, increment) + increment;
}在VarHandle的这些操作方式里面,访问模式会覆盖掉定义时声明的模式,也就是说,如果你声明了一个反射字段句柄,但你使用了普通变量句柄来访问,会导致访问模式变为普通变量的模式。
其他方法
在其余的几个get(),set(),setVolatile()方法内均使用了VarHandle
插入内存屏障的方法来保证数据的一致性。
3.缓存行填充
在Sequence中有一个内部类LhsPadding,里面声明了一堆变量,看起来是这样的:
class LhsPadding
{
protected byte
p10, p11, p12, p13, p14, p15, p16, p17,
p20, p21, p22, p23, p24, p25, p26, p27,
p30, p31, p32, p33, p34, p35, p36, p37,
p40, p41, p42, p43, p44, p45, p46, p47,
p50, p51, p52, p53, p54, p55, p56, p57,
p60, p61, p62, p63, p64, p65, p66, p67,
p70, p71, p72, p73, p74, p75, p76, p77;
}这一堆变量里面,有8*7=56个byte,而在还有个内部类Value继承了LhsPadding,
整体结构这样:

并且还有个内部类RhsPadding又集成了Value,并且他也恰好有8*7=56个byte,而Value的数据类型是long,long为8个字节,所以这个Sequence对象一共有56+8+56=120个byte,而一般缓存行大小是64字节,无论如何分割,这个8个字节的byte都会与左侧填充或者右侧填充达到64个字节,从而避免伪共享问题。
L Value R
| 56 byte | 8 byte | 56 byte |
| 56 byte | 8 byte | 56 byte |
边栏推荐
- 木瓜蛋白酶的特点及相关特异性介绍
- PTA class a simulation bomb 10: 1119-1123
- NLP enhanced technology
- 胶原蛋白酶丨Worthington中英文说明书
- 正则表达式
- Exploring temporary information for dynamic network embedding
- Dataframe extracts data from a column and converts it into a list
- Sweet cool girl jinshuyi was invited to be the spokesperson for the global finals of the sixth season perfect children's model
- Talking about interface test (2)
- 物联网亿万级通信一站式解决方案EMQ
猜你喜欢
随机推荐
分布式系统(二)分布式事务的理解
pixel 6 root
微信朋友圈测试点
秀场精灵陈梓桐 受邀担任第六季完美童模全球总决赛首席体验官
胶原蛋白酶丨Worthington中英文说明书
PTA class a simulated seventh bomb: 1160-1163
Postman斷言對應脚本的解釋
Fasttext knowledge points summary
Test questions and answers for the 2022 baby sitter (Level 5) examination
Dataframe extracts data from a column and converts it into a list
Summary of knowledge points of catboost
阳光男孩陈颢天 受邀担任第六季完美童模全球总决赛代言人
GUN make (4) 规则的命令
Summary of informer's paper
弹性蛋白酶的用途和化学性质
PTA class a simulated sixth bomb: 1156-1159
20. Hough line transformation
Web Testing
求n乘阶之和
Data arrangement of machinetranslation









