Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
6 deletions.
There are no files selected for viewing
15 changes: 9 additions & 6 deletions
15
...rap-core/src/test/java/com/navercorp/pinpoint/bootstrap/interceptor/AtomicMaxUpdater.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,31 @@ | ||
package com.navercorp.pinpoint.bootstrap.interceptor; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; | ||
|
||
/** | ||
* @author emeroad | ||
*/ | ||
public class AtomicMaxUpdater { | ||
|
||
private final AtomicInteger maxIndex = new AtomicInteger(-1); | ||
private static final AtomicIntegerFieldUpdater<AtomicMaxUpdater> UPDATER = AtomicIntegerFieldUpdater.newUpdater(AtomicMaxUpdater.class, "maxIndex"); | ||
|
||
public boolean updateMax(int max) { | ||
private volatile int maxIndex = 0; | ||
|
||
public boolean update(int max) { | ||
while (true) { | ||
final int currentMax = maxIndex.get(); | ||
final int currentMax = getIndex(); | ||
if (currentMax >= max) { | ||
return false; | ||
} | ||
final boolean update = maxIndex.compareAndSet(currentMax, max); | ||
final boolean update = UPDATER.compareAndSet(this, currentMax, max); | ||
if (update) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
|
||
public int getIndex() { | ||
return maxIndex.get(); | ||
return UPDATER.get(this); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...core/src/test/java/com/navercorp/pinpoint/bootstrap/interceptor/AtomicMaxUpdaterTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2014 NAVER Corp. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.navercorp.pinpoint.bootstrap.interceptor; | ||
|
||
import junit.framework.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* @author emeroad | ||
*/ | ||
public class AtomicMaxUpdaterTest { | ||
@Test | ||
public void update() { | ||
AtomicMaxUpdater updater = new AtomicMaxUpdater(); | ||
|
||
Assert.assertFalse(updater.update(0)); | ||
|
||
|
||
Assert.assertTrue(updater.update(1)); | ||
|
||
Assert.assertTrue(updater.update(10)); | ||
|
||
Assert.assertFalse(updater.update(5)); | ||
Assert.assertEquals(updater.getIndex(), 10); | ||
|
||
|
||
} | ||
} |