补充#31部分代码。修复边界BUG。并提交更合理的测试用例。
This commit is contained in:
parent
921ea2723d
commit
e03e9f2583
|
|
@ -21,11 +21,14 @@ public class AtomicRangeInteger extends Number implements java.io.Serializable {
|
|||
*
|
||||
* @param initialValue
|
||||
* the initial value
|
||||
* @param endValue
|
||||
*
|
||||
* AtomicRangeInteger在startValue和maxValue循环取值( startValue <= value < maxValue)
|
||||
*/
|
||||
public AtomicRangeInteger(int startValue, int endValue) {
|
||||
public AtomicRangeInteger(int startValue, int maxValue) {
|
||||
value = new AtomicInteger(startValue);
|
||||
this.startValue = startValue;
|
||||
this.endValue = endValue;
|
||||
this.endValue = maxValue - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -35,11 +38,8 @@ public class AtomicRangeInteger extends Number implements java.io.Serializable {
|
|||
*/
|
||||
public final int getAndIncrement() {
|
||||
for (;;) {
|
||||
int current = get();
|
||||
int next = current + 1;
|
||||
if (next >= this.endValue) {
|
||||
next = this.startValue;
|
||||
}
|
||||
int current = value.get();
|
||||
int next = current >= this.endValue ? this.startValue : current + 1;
|
||||
if (value.compareAndSet(current, next))
|
||||
return current;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,10 @@ import junit.framework.TestCase;
|
|||
import com.ai.cloud.skywalking.util.AtomicRangeInteger;
|
||||
|
||||
public class AtomicRangeIntegerTest extends TestCase{
|
||||
static String[] buffer = new String[5000];
|
||||
|
||||
static AtomicRangeInteger ari = new AtomicRangeInteger(0, buffer.length);
|
||||
|
||||
public void testGet(){
|
||||
AtomicRangeInteger ari = new AtomicRangeInteger(0, 12);
|
||||
for(int i = 0; i < 51; i++){
|
||||
|
|
@ -36,22 +40,20 @@ public class AtomicRangeIntegerTest extends TestCase{
|
|||
}
|
||||
|
||||
class RangeIntegerThread extends Thread{
|
||||
private static String[] buffer = new String[500000000];
|
||||
|
||||
private static AtomicRangeInteger ari = new AtomicRangeInteger(0, buffer.length);
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
while(true){
|
||||
int i = ari.getAndIncrement();
|
||||
int i = AtomicRangeIntegerTest.ari.getAndIncrement();
|
||||
if(i % 10000000 == 0){
|
||||
System.out.println(ari.get());
|
||||
System.out.println(AtomicRangeIntegerTest.ari.get());
|
||||
}
|
||||
if(i >= buffer.length - 100000){
|
||||
if(AtomicRangeIntegerTest.buffer[i] != null){
|
||||
System.out.println("end at index:" + i + "," + AtomicRangeIntegerTest.buffer[i]);
|
||||
break;
|
||||
}else{
|
||||
System.out.println("at index:" + i);
|
||||
AtomicRangeIntegerTest.buffer[i] = "string";
|
||||
}
|
||||
Assert.assertNull(buffer[i]);
|
||||
buffer[i] = "string";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue