That's awesome! After using disruptor in the project, the performance is improved by 2. Download Java intermediate interview questions

mb61ab40ce80d9c 2022-01-15 03:26:50 阅读数:869

awesome using disruptor project performance

here Core-2 It's going to be depressing , Just now I was able to read objects from the cache bar, Now read again and be told that the cache line is invalid , You have to go to the memory and pull it again , Delayed Core-2 Efficiency of execution .

This is the cache pseudo sharing problem , Two unrelated threads execute , Because of the operation of another thread , Cause cache to fail . These two threads are actually competing for the same cache line , Reduced concurrency .

Disruptor Cache row fill

Disruptor To solve the problem of pseudo sharing , The method used is cache row padding . It's a strategy of trading space for time , The main idea is to go to the object

《 A big factory Java Analysis of interview questions + Back end development learning notes + The latest architecture explanation video + Practical project source code handout 》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 Full content open source sharing

Fill in meaningless variables , To ensure that the entire object has exclusive cache lines .

for instance , With Disruptor Medium Sequence For example , stay volatile long value It's placed in front of and behind 7 individual long Type variable , Make sure value Monopolize a cache line .

public?class?Sequence?extends?RhsPadding?{??

private?static?final?long?VALUE_OFFSET;??

static?{??

VALUE_OFFSET?=?UNSAFE.objectFieldOffset(Value.class.getDeclaredField("value"));??

...??

}??

...??

}??

class?RhsPadding?extends?Value?{??

protected?long?p9,?p10,?p11,?p12,?p13,?p14,?p15;??

}??

class?Value?extends?LhsPadding?{??

protected?volatile?long?value;??

}??

class?LhsPadding?{??

protected?long?p1,?p2,?p3,?p4,?p5,?p6,?p7;??

}??

As shown in the figure below , among V Namely Value Class value,P by value It's meaningless to fill before and after long Type variable ,U For other unrelated variables . In any case , Can guarantee V Not in the same cache line as other unrelated variables , such V It won't be affected by other irrelevant variables .

 It's awesome ! Used in the project Disruptor after , Improved performance 2,java Intermediate interview questions download

picture

Padding fill

there V It is not limited to long type , In fact, as long as the size of the object is greater than or equal to 8 Bytes , By filling the front and back 7 individual long Type variable , It must be able to guarantee exclusive cache lines .

Here to Disruptor Of RingBuffer For example , The leftmost 7 individual long Type variables are defined in the top-level parent class RingBufferPad in , Far right 7 individual long Type variables are defined in RingBuffer In the last line of the variable definition , So all the variables that need to be exclusive are left and right long I'm surrounded by , Make sure the cache lines are exclusive .

public?final?class?RingBuffer<E>?extends?RingBufferFields<E>?implements?Cursored,?EventSequencer<E>,?EventSink<E>?{??

public?static?final?long?INITIAL_CURSOR_VALUE?=?Sequence.INITIAL_VALUE;??

protected?long?p1,?p2,?p3,?p4,?p5,?p6,?p7;??

...??

}??

abstract?class?RingBufferFields<E>?extends?RingBufferPad??

{??

...??

}??

abstract?class?RingBufferPad?{??

protected?long?p1,?p2,?p3,?p4,?p5,?p6,?p7;??

}??

@Contended

stay JDK 1.8 in , Provides @sun.misc.Contended annotation , Using this annotation, you can make the variable own the cache line , No more manual filling . Be careful ,JVM Need to add parameters -XX:-RestrictContended To enable this function .

If the annotation is defined on a class , Each variable representing the class has exclusive cache lines ; If it's defined on a variable , By designation groupName, same groupName Will monopolize the same cache line .

//? Class plus each variable representing the entire class will be in a separate cache?line in ??

@sun.misc.Contended??

public?class?ContendedData?{??

int?value;??

long?modifyTime;??

boolean?flag;??

long?createTime;??

char?key;??

}??

//? same ?groupName? In the same cache line ??

public?class?ContendedGroupData?{??

@sun.misc.Contended("group1")??

int?value;??

@sun.misc.Contended("group1")??

long?modifyTime;??

@sun.misc.Contended("group2")??

boolean?flag;??

@sun.misc.Contended("group3")??

long?createTime;??

@sun.misc.Contended("group3")??

char?key;??

}??

@Contended stay JDK It has been applied in the source code , With Thread Class, for example , In order to ensure that the operation of random number in multithreading will not produce pseudo sharing , The related variables are set to the same groupName.

public?class?Thread?implements?Runnable?{??

...??

//?The?following?three?initially?uninitialized?fields?are?exclusively??

//?managed?by?class?java.util.concurrent.ThreadLocalRandom.?These??

//?fields?are?used?to?build?the?high-performance?PRNGs?in?the??

//?concurrent?code,?and?we?can?not?risk?accidental?false?sharing.??

//?Hence,[email protected]??

/*?The?current?seed?for?a?ThreadLocalRandom?/??

@sun.misc.Contended("tlr")??

long?threadLocalRandomSeed;??

/*?Probe?hash?value;?nonzero?if?threadLocalRandomSeed?initialized?/??

@sun.misc.Contended("tlr")??

int?threadLocalRandomProbe;??

/*?Secondary?seed?isolated?from?public?ThreadLocalRandom?sequence?/??

@sun.misc.Contended("tlr")??

int?threadLocalRandomSecondarySeed;??

...??

}??

How to quickly update your technology accumulation ?

  • In existing projects , Dig deep , For example, we use netty You can look at the underlying code and the main points .
  • If you don't know the direction of the current efforts , It depends on what the leaders or the skilled people in the company are learning .
  • I don't know how to learn after I know the direction of my efforts , Just go around looking for relevant information and practice .
  • After studying, I don't know if I have learned , You can test it through an interview .

I personally think the interview is like a new journey , Failure and victory are common . therefore , Don't be discouraged by the failure of the interview 、 The loss of morale . Don't be complacent about passing the interview , Waiting for you will be a better future , Continue refueling !

The answers to the above interview topics are arranged into interview documents , The answer is detailed in the document , And some other interview questions from big companies

 Eight years CRUD, Three months to prepare for the epidemic , Three headlines 、 It's on all sides offer Face to face sharing

 Eight years CRUD, Three months to prepare for the epidemic , Three headlines 、 It's on all sides offer Face to face sharing

This article has been  CODING Open source project :【 A big factory Java Analysis of interview questions + Core summary learning notes + The latest explanation video + Actual project source code 】 Included

版权声明:本文为[mb61ab40ce80d9c]所创,转载请带上原文链接,感谢。 https://javamana.com/2021/12/202112122307167282.html