Last active
December 18, 2015 10:58
-
-
Save qinxian/5771879 to your computer and use it in GitHub Desktop.
Modest-Lock for Disruptor's WaitStrategy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2011 LMAX Ltd. and qinxian | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
* | |
* Written by qinxian. | |
*/ | |
package com.lmax.disruptor; | |
/** | |
* Yielding strategy that uses a Thread.yield() for {@link com.lmax.disruptor.EventProcessor}s waiting on a barrier | |
* after an initially spinning. | |
* | |
* This strategy is a good compromise between performance and CPU resource without incurring significant latency spikes. | |
*/ | |
public final class ModestLockWaitStrategy implements WaitStrategy | |
{ | |
private static final int SPIN_TRIES = 100; | |
@Override | |
public long waitFor(final long sequence, Sequence cursor, final Sequence dependentSequence, final SequenceBarrier barrier) | |
throws AlertException, InterruptedException | |
{ | |
long availableSequence; | |
int counter = SPIN_TRIES; | |
while ((availableSequence = dependentSequence.get()) < sequence) | |
{ | |
counter = applyWaitMethod(barrier, counter); | |
} | |
return availableSequence; | |
} | |
@Override | |
public void signalAllWhenBlocking() | |
{ | |
} | |
private int applyWaitMethod(final SequenceBarrier barrier, int counter) | |
throws AlertException | |
{ | |
barrier.checkAlert(); | |
if ((counter&1)==1) | |
{ | |
Thread.yield(); | |
} | |
return --counter; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
my test result show the benefit:
Run 0, Disruptor=806,451,612 ops/sec
Run 1, Disruptor=821,018,062 ops/sec
Run 2, Disruptor=1,122,964,626 ops/sec
Run 3, Disruptor=1,164,144,353 ops/sec
Run 4, Disruptor=1,133,144,475 ops/sec
Run 5, Disruptor=1,186,239,620 ops/sec
Run 6, Disruptor=1,175,088,131 ops/sec
Run 7, Disruptor=1,143,510,577 ops/sec
Run 8, Disruptor=1,174,398,120 ops/sec
Run 9, Disruptor=1,153,402,537 ops/sec
Run 10, Disruptor=1,154,068,090 ops/sec
Run 11, Disruptor=1,175,088,131 ops/sec
Run 12, Disruptor=1,133,144,475 ops/sec
Run 13, Disruptor=1,049,868,766 ops/sec
Run 14, Disruptor=1,094,690,749 ops/sec
Run 15, Disruptor=1,164,144,353 ops/sec
Run 16, Disruptor=1,186,239,620 ops/sec
Run 17, Disruptor=1,219,512,195 ops/sec
Run 18, Disruptor=1,207,729,468 ops/sec
Run 19, Disruptor=1,196,888,090 ops/sec
//mybusyspin
Run 0, Disruptor=1,583,531,274 ops/sec
Run 1, Disruptor=1,454,545,454 ops/sec
Run 2, Disruptor=1,803,426,510 ops/sec
Run 3, Disruptor=1,728,608,470 ops/sec
Run 4, Disruptor=1,777,777,777 ops/sec
Run 5, Disruptor=1,728,608,470 ops/sec
Run 6, Disruptor=1,908,396,946 ops/sec
Run 7, Disruptor=1,754,385,964 ops/sec
Run 8, Disruptor=1,937,984,496 ops/sec
Run 9, Disruptor=1,706,484,641 ops/sec
Run 10, Disruptor=1,801,801,801 ops/sec
Run 11, Disruptor=1,776,198,934 ops/sec
Run 12, Disruptor=1,855,287,569 ops/sec
Run 13, Disruptor=1,828,153,564 ops/sec
Run 14, Disruptor=1,471,670,345 ops/sec
Run 15, Disruptor=1,801,801,801 ops/sec
Run 16, Disruptor=1,752,848,378 ops/sec
Run 17, Disruptor=1,910,219,675 ops/sec
Run 18, Disruptor=1,828,153,564 ops/sec
Run 19, Disruptor=1,855,287,569 ops/sec
Run 0, Concurrentor=1,644,736,842 ops/sec
Run 1, Concurrentor=1,640,689,089 ops/sec
Run 2, Concurrentor=1,968,503,937 ops/sec
Run 3, Concurrentor=1,968,503,937 ops/sec
Run 4, Concurrentor=1,968,503,937 ops/sec
Run 5, Concurrentor=1,968,503,937 ops/sec
Run 6, Concurrentor=1,998,001,998 ops/sec
Run 7, Concurrentor=1,968,503,937 ops/sec
Run 8, Concurrentor=1,968,503,937 ops/sec
Run 9, Concurrentor=1,968,503,937 ops/sec
Run 10, Concurrentor=2,000,000,000 ops/sec
Run 11, Concurrentor=1,968,503,937 ops/sec
Run 12, Concurrentor=1,968,503,937 ops/sec
Run 13, Concurrentor=1,968,503,937 ops/sec
Run 14, Concurrentor=1,968,503,937 ops/sec
Run 15, Concurrentor=2,000,000,000 ops/sec
Run 16, Concurrentor=2,000,000,000 ops/sec
Run 17, Concurrentor=2,000,000,000 ops/sec
Run 18, Concurrentor=1,968,503,937 ops/sec
Run 19, Concurrentor=1,968,503,937 ops/sec
Run 0, Concurrentor=1,806,684,733 ops/sec
Run 1, Concurrentor=1,706,484,641 ops/sec
Run 2, Concurrentor=2,000,000,000 ops/sec
Run 3, Concurrentor=2,004,008,016 ops/sec
Run 4, Concurrentor=1,998,001,998 ops/sec
Run 5, Concurrentor=2,030,456,852 ops/sec
Run 6, Concurrentor=2,036,659,877 ops/sec
Run 7, Concurrentor=2,030,456,852 ops/sec
Run 8, Concurrentor=2,000,000,000 ops/sec
Run 9, Concurrentor=1,972,386,587 ops/sec
Run 10, Concurrentor=1,998,001,998 ops/sec
Run 11, Concurrentor=2,030,456,852 ops/sec
Run 12, Concurrentor=2,036,659,877 ops/sec
Run 13, Concurrentor=1,998,001,998 ops/sec
Run 14, Concurrentor=2,000,000,000 ops/sec
Run 15, Concurrentor=2,002,002,002 ops/sec
Run 16, Concurrentor=2,030,456,852 ops/sec
Run 17, Concurrentor=2,034,587,995 ops/sec
Run 18, Concurrentor=2,000,000,000 ops/sec
Run 19, Concurrentor=2,002,002,002 ops/sec