未验证 提交 40adaf76 编写于 作者: 张皮皮 提交者: GitHub

[ISSUE #2652] change the method name to incrementAndGet

Co-authored-by: N张玻 <zhangbo@mydataway.com>
上级 97c77701
...@@ -23,7 +23,7 @@ public class ThreadLocalIndex { ...@@ -23,7 +23,7 @@ public class ThreadLocalIndex {
private final ThreadLocal<Integer> threadLocalIndex = new ThreadLocal<Integer>(); private final ThreadLocal<Integer> threadLocalIndex = new ThreadLocal<Integer>();
private final Random random = new Random(); private final Random random = new Random();
public int getAndIncrement() { public int incrementAndGet() {
Integer index = this.threadLocalIndex.get(); Integer index = this.threadLocalIndex.get();
if (null == index) { if (null == index) {
index = Math.abs(random.nextInt()); index = Math.abs(random.nextInt());
......
...@@ -71,7 +71,7 @@ public class TopicPublishInfo { ...@@ -71,7 +71,7 @@ public class TopicPublishInfo {
return selectOneMessageQueue(); return selectOneMessageQueue();
} else { } else {
for (int i = 0; i < this.messageQueueList.size(); i++) { for (int i = 0; i < this.messageQueueList.size(); i++) {
int index = this.sendWhichQueue.getAndIncrement(); int index = this.sendWhichQueue.incrementAndGet();
int pos = Math.abs(index) % this.messageQueueList.size(); int pos = Math.abs(index) % this.messageQueueList.size();
if (pos < 0) if (pos < 0)
pos = 0; pos = 0;
...@@ -85,7 +85,7 @@ public class TopicPublishInfo { ...@@ -85,7 +85,7 @@ public class TopicPublishInfo {
} }
public MessageQueue selectOneMessageQueue() { public MessageQueue selectOneMessageQueue() {
int index = this.sendWhichQueue.getAndIncrement(); int index = this.sendWhichQueue.incrementAndGet();
int pos = Math.abs(index) % this.messageQueueList.size(); int pos = Math.abs(index) % this.messageQueueList.size();
if (pos < 0) if (pos < 0)
pos = 0; pos = 0;
......
...@@ -80,7 +80,7 @@ public class LatencyFaultToleranceImpl implements LatencyFaultTolerance<String> ...@@ -80,7 +80,7 @@ public class LatencyFaultToleranceImpl implements LatencyFaultTolerance<String>
if (half <= 0) { if (half <= 0) {
return tmpList.get(0).getName(); return tmpList.get(0).getName();
} else { } else {
final int i = this.whichItemWorst.getAndIncrement() % half; final int i = this.whichItemWorst.incrementAndGet() % half;
return tmpList.get(i).getName(); return tmpList.get(i).getName();
} }
} }
......
...@@ -58,7 +58,7 @@ public class MQFaultStrategy { ...@@ -58,7 +58,7 @@ public class MQFaultStrategy {
public MessageQueue selectOneMessageQueue(final TopicPublishInfo tpInfo, final String lastBrokerName) { public MessageQueue selectOneMessageQueue(final TopicPublishInfo tpInfo, final String lastBrokerName) {
if (this.sendLatencyFaultEnable) { if (this.sendLatencyFaultEnable) {
try { try {
int index = tpInfo.getSendWhichQueue().getAndIncrement(); int index = tpInfo.getSendWhichQueue().incrementAndGet();
for (int i = 0; i < tpInfo.getMessageQueueList().size(); i++) { for (int i = 0; i < tpInfo.getMessageQueueList().size(); i++) {
int pos = Math.abs(index++) % tpInfo.getMessageQueueList().size(); int pos = Math.abs(index++) % tpInfo.getMessageQueueList().size();
if (pos < 0) if (pos < 0)
...@@ -74,7 +74,7 @@ public class MQFaultStrategy { ...@@ -74,7 +74,7 @@ public class MQFaultStrategy {
final MessageQueue mq = tpInfo.selectOneMessageQueue(); final MessageQueue mq = tpInfo.selectOneMessageQueue();
if (notBestBroker != null) { if (notBestBroker != null) {
mq.setBrokerName(notBestBroker); mq.setBrokerName(notBestBroker);
mq.setQueueId(tpInfo.getSendWhichQueue().getAndIncrement() % writeQueueNums); mq.setQueueId(tpInfo.getSendWhichQueue().incrementAndGet() % writeQueueNums);
} }
return mq; return mq;
} else { } else {
......
...@@ -387,7 +387,7 @@ public class AsyncTraceDispatcher implements TraceDispatcher { ...@@ -387,7 +387,7 @@ public class AsyncTraceDispatcher implements TraceDispatcher {
filterMqs.add(queue); filterMqs.add(queue);
} }
} }
int index = sendWhichQueue.getAndIncrement(); int index = sendWhichQueue.incrementAndGet();
int pos = Math.abs(index) % filterMqs.size(); int pos = Math.abs(index) % filterMqs.size();
if (pos < 0) { if (pos < 0) {
pos = 0; pos = 0;
......
...@@ -22,17 +22,17 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -22,17 +22,17 @@ import static org.assertj.core.api.Assertions.assertThat;
public class ThreadLocalIndexTest { public class ThreadLocalIndexTest {
@Test @Test
public void testGetAndIncrement() throws Exception { public void testIncrementAndGet() throws Exception {
ThreadLocalIndex localIndex = new ThreadLocalIndex(); ThreadLocalIndex localIndex = new ThreadLocalIndex();
int initialVal = localIndex.getAndIncrement(); int initialVal = localIndex.incrementAndGet();
assertThat(localIndex.getAndIncrement()).isEqualTo(initialVal + 1); assertThat(localIndex.incrementAndGet()).isEqualTo(initialVal + 1);
} }
@Test @Test
public void testGetAndIncrement2() throws Exception { public void testIncrementAndGet2() throws Exception {
ThreadLocalIndex localIndex = new ThreadLocalIndex(); ThreadLocalIndex localIndex = new ThreadLocalIndex();
int initialVal = localIndex.getAndIncrement(); int initialVal = localIndex.incrementAndGet();
assertThat(initialVal >= 0); assertThat(initialVal >= 0);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册