mb61ab40ce80d9c 2022-01-15 03:26:50 阅读数:869
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 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 .
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 .
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 .
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 ??
//? same ?groupName? In the same cache line ??
@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.
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